---
title: "Clearning Pew Global Attitudes Project"
---

# Load

```{r}
# load packages
  source("helper-packages.R")

# load pew global attitudes spring 2004
  pew_gap_spring_04_raw <-
    read_sav("../raw-data/y-multi-pew-global-attitudes/Pew-GAP-March-2004-survey-for-website/Pew Global Attitudes 2004 FINAL DATASET.sav")

# load pew global attitudes 2005 (6 countries)
  pew_gap_spring_05_6countries_raw <-
    read_sav("../raw-data/y-multi-pew-global-attitudes/Pew-GAP-Spring-2005-survey-for-website/Pew Global Attitudes 2005 6-COUNTRY DATASET.sav")

# load pew global attitudes 2005 (11 countries)
  pew_gap_spring_05_11countries_raw <-
    read_sav("../raw-data/y-multi-pew-global-attitudes/Pew-GAP-Spring-2005-survey-for-website/Pew Global Attitudes 2005 11-COUNTRY DATASET.sav")

# load pew global attitudes spring 2006
  pew_gap_spring_06_raw <-
    read_sav("../raw-data/y-multi-pew-global-attitudes/Pew-Global-Attitudes_Spring-2006-15-Country-Dataset/Pew Global Attitudes 2006 15-Country Dataset.sav")
  
# load pew global attitudes spring 2007
  pew_gap_spring_07_raw <-
    read_sav("../raw-data/y-multi-pew-global-attitudes/Pew-GAP-Spring-2007-survey-for-website/GAP_2007_Data.sav", encoding = "latin1")

# load pew global attitudes spring 2008
  pew_gap_spring_08_raw <-
    read_sav("../raw-data/y-multi-pew-global-attitudes/Pew-GAP-Spring-2008-survey-for-website/GAP_2008_Dataset_release.sav")

#load pew global attitudes spring 2009
  pew_gap_spring_09_raw <-
    read_sav("../raw-data/y-multi-pew-global-attitudes/Pew-GAP-Spring-2009-survey-for-website/Pew Gap 2009 Data.sav", encoding = "latin1")

# load pew global attitudes fall 2009
  pew_gap_fall_09_raw <-
    read_sav("../raw-data/y-multi-pew-global-attitudes/Pew-GAP-Fall-2009-BW-survey-for-website/Pew Global Attitudes Fall 2009 Dataset.sav", encoding = "latin1")
  
# load pew global attitudes spring 2010
  pew_gap_spring_10_raw <-
    read_sav("../raw-data/y-multi-pew-global-attitudes/Pew-GAP-Spring-2010-survey-for-website/Pew Research Center Global Attitudes Spring 2010 Dataset WEB.sav")
  
# load pew global attitudes spring 2011
  pew_gap_spring_11_raw <-
    read_sav("../raw-data/y-multi-pew-global-attitudes/Pew-Global-Attitudes-Spring-2011-Survey/Pew Global Attitudes Spring 2011 Dataset WEB.sav")

# load pew global attitudes spring 2014
  pew_gap_spring_14_raw <-
    read_sav("../raw-data/y-multi-pew-global-attitudes/Pew.Research.Center.Spring.2014.Global.Attitudes.Dataset.for_.Web_/Pew Research Global Attitudes Spring 2014 Dataset for Web.sav")

# load pew global attitudes spring 2015 [RELATIONAL FILEPATH NOT WORKING WHY??]
  pew_gap_spring_15_raw <- 
    read_sav("../raw-data/y-multi-pew-global-attitudes/Pew-Research-Global-Attitudes-Spring-2015-Dataset-for-Web-FINAL/Pew Research Global Attitudes Spring 2015 Dataset for Web FINAL.sav")
  
# load pew global attitudes spring 2016 [RELATIONAL FILEPATH NOT WORKING WHY??]
  pew_gap_spring_16_raw <-
    read_sav("../raw-data/y-multi-pew-global-attitudes/Pew-Research-Center-Spring-2016-Global-Attitudes-Survey-WEB-FINAL/Pew Research Center Spring 2016 Global Attitudes Dataset WEB FINAL.sav")
 
# load pew global attitudes spring 2019
  pew_gap_spring_19_raw <-
    read_sav("../raw-data/y-multi-pew-global-attitudes/Pew-Research-Center-Global-Attitudes-Spring-2019-Survey-Data/Pew Research Center Global Attitudes Spring 2019 Dataset WEB.sav")

# load manually coded resp_religion (based on resp_denomination)
  cw_resp_religion <-
    import("../raw-data/y-multi-pew-global-attitudes/_coding_resp_religion/pga_coding_resp_religion.csv")
```

# Clean Pew Global Attitudes Summer 2002:

-NOT PROCESSED:
--REASONS:
---No question on religious intolerance

# Clean Pew Global Attitudes Novermber 2002:

-NOT PROCESSED:
--REASONS:
---No question on religious intolerance

# Clean Pew Global Attitudes March 2003:

-NOT PROCESSED:
--REASONS:
---No question on religious intolerance

# Clean Pew Global Attitudes May 2003

-NOT PROCESSED:
--REASONS:
---No question on religious intolerance

# Clean Pew Global Attitudes Spring 2007

-NOT PROCESSED:
--REASONS:
---No question on religious intolerance

# Clean Pew Global Attitudes Spring 2012

-NOT PROCESSED:
--REASONS:
---No question on religious intolerance

# Clean Pew Global Attitudes Spring 2013

-NOT PROCESSED:
--REASONS:
---No question on religious intolerance

# Clean Pew Global Attitudes Winter 2014 India

-NOT PROCESSED:
--REASONS:
---No question on religious intolerance

# Clean Pew Global Attitudes Spring 2017

-NOT PROCESSED:
--REASONS:
---Unusual: only questions on religious tolerance for Italy, but Italy lacks a variable on respondent religious, so cannot be used

# Clean Pew Global Attitudes Spring 2018

-NOT PROCESSED:
--REASONS:
---No question on religious intolerance

# Clean Pew Global Attitudes Summer 2020

-NOT PROCESSED:
--REASONS:
---No question on religious intolerance

# Clean Pew Global Attitudes Spring 2004

* No religion variable for Morocco

```{r}
# declare dates
  dates_pew_gap_spring_04 <-
    tribble(
       ~resp_country_common, ~resp_interview_start_date, ~resp_interview_end_date,
       "UK", "2004-02-23", "2004-02-29",
       "France", "2004-02-23", "2004-02-28",
       "Germany", "2004-02-23", "2004-02-29",
       "Jordan", "2004-02-24", "2004-02-29",
       "Morocco", "2004-02-19", "2004-02-24",
       "Pakistan", "2004-02-23", "2004-03-03",
       "Russia", "2004-02-20", "2004-02-29",
       "Turkey", "2004-02-21", "2004-03-01",
       "US", "2004-02-24", "2004-02-29"
    ) %>%
    mutate(
        resp_interview_start_date = as.Date(resp_interview_start_date, "%Y-%m-%d"),
        resp_interview_end_date = as.Date(resp_interview_end_date, "%Y-%m-%d"))

# clean pew global attitudes spring 2004
  n1_clean_pew_gap_spring_04 <-
    pew_gap_spring_04_raw %>% 
    mutate(
      
    #########################  
    ####### META-DATA #######  
    #########################      
      
      # source name (character vector, title case)
        resp_source = "Pew Global Attitudes Project",
        
      # round number (character vector, title case)  
        resp_round = "Spring 2004",
      
      # url to dataset source, where publicly available (character vector)
        resp_original_data_url = "pewrsr.ch/3wToJeP",

      # country (character vector; list of countries as written in original source)
        resp_country_original = to_character(country),
    
      # survey mode (in-person/phone/internet/mail)
        # Results for the survey of 9 countries are based on telephone and face-to-face interviews conducted under
        # the direction of Princeton Survey Research Associates International. Telephone interviews were
        # conducted among a nationwide, representative sample of 1,000 adults, 18 years of age or older, in the
        # United States, 500 in Great Britain, 503 in France, and 500 in Germany. The fieldwork was conducted by
        # NOP in Great Britain between February 23-29, 2004, by IFOP in France between February 23-28, 2004,
        # by TNS-Emnid in Germany between February 23-29, 2004, and by Princeton Data Source in the U.S.
        # between February 24-29, 2004.
        # Face-to-face interviews were conducted among a nation-wide representative sample of 1,000 adults in
        # Jordan, 1,002 in Russia, and 1,017 in Turkey. The fieldwork was conducted by MRO in Jordan between
        # February 24-29, 2004, by ROMIR in Russia between February 20-29, 2004, and by TNS Piar in Turkey
        # between February 21 and March 1, 2004. In Morocco, 1,000 face-to-face interviews of adults 18-59 years
        # of age were conducted in four major cities (Casablanca, Rabat, Fes and Marrakech) by Synovate Market
        # Research between February 19-24, 2004. In Pakistan, 1,242 predominantly urban face-to-face interviews
        # were conducted by ACNielsen between February 23-March 3, 2004.    
    
        resp_survey_mode = 
          case_when(
            resp_country_original %in% c("United States", "Britain", "France", "Germany") ~ "phone",
            resp_country_original %in% c("Jordan", "Russia", "Turkey", "Morocco", "Pakistan") ~ "in-person"),
    
      # country (character vector; converts to countrycode county.name list)
        resp_country_common = 
          countryname(resp_country_original),
        
      # interview date (variable of class Date; if only month given, input 1st of month)
        resp_interview_date = as.Date(as.character(date), "%d%m%y")) %>%
    left_join(dates_pew_gap_spring_04, by = "resp_country_common") %>%
    mutate(
   
    #########################  
    ##### DEMOGRAPHICS ######  
    #########################
    
     # respondent's denomination (recoded jointly below)
        q32usa = relig,
        across(starts_with("q32"), to_character)) %>% 
    mutate(
        resp_denomination = coalesce(!!! select(., starts_with("q32"))),

      # respondent's age (character vector; bins denoted by single dash ["18-25"])
        resp_age = # not available for all countries and split across variables
          case_when(
            recage == 1 ~ "18-24", #us
            recage == 2 ~ "25-34",
            recage == 3 ~ "35-44",
            recage == 4 ~ "45-54",
            recage == 5 ~ "55-64",
            recage == 6 ~ "65+",
            
            agetur == 1 ~ "18-24", #turkey
            agetur == 2 ~ "25-34",
            agetur == 3 ~ "35-44",
            agetur == 4 ~ "45-54",
            agetur == 5 ~ "55+",
            
            TRUE ~ as.character(Q31)), #rest        
      
      # respondent's education level
        resp_education_original =
          case_when(
            #Britain
            q33brit == 1 ~ "1. No formal education [No education]",
            q33brit == 2 ~ "2. Incomplete primary school [No education]",
            q33brit == 3 ~ "3. Complete primary school [Primary]",
            q33brit == 4 ~ "4. Incomplete secondary school: GCSE/ O`Levels [Primary]",
            q33brit == 5 ~ "5. Complete secondary school: GCSE/ O`Levels [Primary]",
            q33brit == 6 ~ "6. Incomplete secondary: university-preparatory type (A-Level) [Primary]",
            q33brit == 7 ~ "7. Complete secondary: university-preparatory type (A-Level) [Primary]",
            q33brit == 8 ~ "8. Incomplete university/ polytechnic: Degree/HND [Primary]",
            q33brit == 9 ~ "9. Complete University / Polytechnic, with degree [College]",
            q33brit == 10 ~ "10. Post graduate degree level [College]",
            #France
            q33fra == 1 ~ "1. Sans diplôme [No education]",
            q33fra == 2 ~ "2. Certificat d'études primaires [Primary]",
            q33fra == 3 ~ "3. Ancien Brevet, B.E.P.C. [Primary]",
            q33fra == 4 ~ "4. Certificat d'aptitudes professionnel (C.A.P.) [Primary]",
            q33fra == 5 ~ "5. Brevet d'enseignement professionnel (B.E.P.) [Primary]",
            q33fra == 6 ~ "6. Baccalauréat d'enseignement technique [Primary]", 
            q33fra == 7 ~ "7. Baccalauréat d'enseignement général [College]", # best guess: note there may be a labeling error here (no aid from questionnaire); only 6 levels actually represented in data
            q33fra == 8 ~ "8. Bac + 2 ans ou niveau Bac+2 ans (DUT, BTS, DEUG, instituteur [College]",
            q33fra == 9 ~ "9. Diplôme de l'enseignement supérieur (2ème, 3ème cycle, grand [College]",
            #Germany
            q33ger == 11 ~ "11. Still attending school [Primary]", # note unusual numbering
            q33ger == 12 ~ "12. Lower secondary school without professional training [Primary]",
            q33ger == 13 ~ "13. Lower secondary school with professional training [Primary]",
            q33ger == 14 ~ "14. Middle secondary school without matriculation standard [Primary]",
            q33ger == 15 ~ "15. General qualification for university entrance [Primary]",
            q33ger == 16 ~ "16. University degree [College]",
            #Jordan
            q33jor == 1 ~ "1. No formal education [No education]",
            q33jor == 2 ~ "2. Incomplete primary school [No education]",
            q33jor == 3 ~ "3. Complete primary school [Primary]",
            q33jor == 4 ~ "4. Incomplete secondary school: technical/vocational type [Primary]",
            q33jor == 5 ~ "5. Complete secondary school: technical/vocational type [Primary]",
            q33jor == 6 ~ "6. Incomplete secondary: university-preparatory type [Primary]",
            q33jor == 7 ~ "7. Complete secondary: university-preparatory type [Primary]",
            q33jor == 8 ~ "8. Some university-level education, without a degree [Primary]",
            q33jor == 9 ~ "9. University-level education, with degree [College]",
            #Morocco
            q33mor == 1 ~ "1. No formal education [No education]",
            q33mor == 2 ~ "2. Incomplete primary school [No education]",
            q33mor == 3 ~ "3. Complete primary school [Primary]",
            q33mor == 4 ~ "4. Incomplete secondary school: technical/vocational type [Primary]",
            q33mor == 5 ~ "5. Complete secondary school: technical/vocational type [Primary]",
            q33mor == 6 ~ "6. Incomplete secondary: university-preparatory type [Primary]",
            q33mor == 7 ~ "7. Complete secondary: university-preparatory type [Primary]",
            q33mor == 8 ~ "8. Some university-level education, without a degree [Primary]",
            q33mor == 9 ~ "9. University-level education, with degree [College]",
            #Pakistan
            q33pak == 1 ~ "1. no formal education [No education]",
            q33pak == 2 ~ "2. No formal education but can read and write [No education]",
            q33pak == 3 ~ "3. Less than primary [No education]",
            q33pak == 4 ~ "4. school 5-9 years [Primary]",
            q33pak == 5 ~ "5. Matric [Primary]",
            q33pak == 6 ~ "6. Intermediate [Primary]",
            q33pak == 7 ~ "7. Graduate [College]",
            q33pak == 8 ~ "8. Post-graduate [College]",
            #Russia
            q33rus == 1 ~ "1. No formal education [No education]",
            q33rus == 2 ~ "2. Incomplete primary school [No education]",
            q33rus == 3 ~ "3. Complete primary school [Primary]",
            q33rus == 4 ~ "4. Incomplete secondary school: technical/vocational type [Primary]",
            q33rus == 5 ~ "5. Complete secondary school: technical/vocational type [Primary]",
            q33rus == 6 ~ "6. Incomplete secondary: university-preparatory type [Primary]",
            q33rus == 7 ~ "7. Complete secondary: university-preparatory type [Primary]",
            q33rus == 8 ~ "8. Some university-level education, without a degree [Primary]",
            q33rus == 9 ~ "9. University-level education, with degree [College]",
            #Turkey
            q33tur == 1 ~ "1. No formal education [No education]",
            q33tur == 2 ~ "2. Incomplete primary school [No education]",
            q33tur == 3 ~ "3. Complete primary school [Primary]",
            q33tur == 4 ~ "4. Incomplete secondary school: technical/vocational type [Primary]",
            q33tur == 5 ~ "5. Complete secondary school: technical/vocational type [Primary]",
            q33tur == 6 ~ "6. Incomplete secondary: university-preparatory type [Primary]",
            q33tur == 7 ~ "7. Complete secondary: university-preparatory type [Primary]",
            q33tur == 8 ~ "8. Some university-level education, without a degree [Primary]",
            q33tur == 9 ~ "9. University-level education, with degree [College]",
            #United States   
            educ == 1 ~ "1. None, or grade 1-8 [Primary]",
            educ == 2 ~ "2. High school incomplete [Primary]",
            educ == 3 ~ "3. High school graduate [Primary]",
            educ == 4 ~ "4. Business, Technical, or vocational school AFTER high school [Primary]",
            educ == 5 ~ "5. Some college, no 4-year degree [Primary]",
            educ == 6 ~ "6. College graduate [College]",
            educ == 7 ~ "7. Post-graduate training or professional schooling after college [College]",
            # all else NA
            TRUE ~ NA_character_),    
      
      # respondent's gender (numeric: female = 1; male = 0; other = NA)
        resp_female = 
          case_when(
            q30 == 2 ~ 1,
            q30 == 1 ~ 0,
            TRUE ~ NA_real_),
      
      # respondent resident in rural (vs urban) area (numeric: rural = 1; urban/semi-urban/peri-urban = 0)
        resp_rural =
          case_when(
            Urtur == 2 ~ 1, # turkey
            Urtur == 1 ~ 0,
            
            urpak == 2 ~ 1, # pakistan
            urpak == 1 ~ 0,
            
            citysize == 4 ~ 1,
            citysize %in% c(1:3) ~ 0,
            
            TRUE ~ NA_real_), # only available for 3 countries
      
    #########################  
    ### SOCIAL DISTANCE 1 ###  
    #########################
    
      # original question number; question text; response options (input above)
        resp_soc_dist_1_qinfo = "NUM: Q2 [q2e]; QTEXT: Please tell me if you have a very favorable, somewhat favorable, somewhat unfavorable or very unfavorable opinion of Jews?; ROPTIONS: 1 = Very favorable [=0] + 2 = Somewhat favorable [=0] + 3 = Somewhat unfavorable [=1] + 4 = Very unfavorable [=1]; TARGET: Jewish; TYPE: Favorability",
      
      # original response (as character vector)
        resp_soc_dist_1_original = 
          dplyr::recode(
            as.character(q2e),
            "5" = NA_character_, #Not in the questionnaire
            "8" = NA_character_,
            "9" = NA_character_),       

      # binary recode (numeric: 1 = any negative attitude expressed; 0 otherwise)
        resp_soc_dist_1_bin_recode = 
          case_when(
            q2e %in% c(1:2) ~ 0,
            q2e %in% c(3:4) ~ 1,
            TRUE ~ NA_real_),
    
    #########################  
    ### SOCIAL DISTANCE 2 ###  
    #########################
    
      # original question number; question text; response options (input above)
        resp_soc_dist_2_qinfo = "NUM: Q2 [q2f]; QTEXT: Please tell me if you have a very favorable, somewhat favorable, somewhat unfavorable or very unfavorable opinion of Christians?; ROPTIONS: 1 = Very favorable [=0] + 2 = Somewhat favorable [=0] + 3 = Somewhat unfavorable [=1] + 4 = Very unfavorable [=1]; TARGET: Christian; TYPE: Favorability",
      
      # original response (as character vector)
        resp_soc_dist_2_original = 
          dplyr::recode(
            as.character(q2f),
            "8" = NA_character_,
            "9" = NA_character_),       

      # binary recode (numeric: 1 = any negative attitude expressed; 0 otherwise)
        resp_soc_dist_2_bin_recode = 
          case_when(
            q2f %in% c(1:2) ~ 0,
            q2f %in% c(3:4) ~ 1,
            TRUE ~ NA_real_),
    
    #########################  
    ### SOCIAL DISTANCE 3 ###  
    #########################
    
      # original question number; question text; response options (input above)
        resp_soc_dist_3_qinfo = "NUM: Q2 [q2g]; QTEXT: Please tell me if you have a very favorable, somewhat favorable, somewhat unfavorable or very unfavorable opinion of Muslims?; ROPTIONS: 1 = Very favorable [=0] + 2 = Somewhat favorable [=0] + 3 = Somewhat unfavorable [=1] + 4 = Very unfavorable [=1]; TARGET: Muslim; TYPE: Favorability",
      
      # original response (as character vector)
        resp_soc_dist_3_original = 
          dplyr::recode(
            as.character(q2g),
            "5" = NA_character_, #Not in the questionnaire
            "8" = NA_character_,
            "9" = NA_character_),       

      # binary recode (numeric: 1 = any negative attitude expressed; 0 otherwise)
        resp_soc_dist_3_bin_recode = 
          case_when(
            q2g %in% c(1:2) ~ 0,
            q2g %in% c(3:4) ~ 1,
            TRUE ~ NA_real_),
    
    ############################  
    ### GENERAL SOCIAL TRUST ###  
    ############################
    
      # original question number; question text; response options (input above)
        resp_gentrust_qinfo = NA, # checked; no question asked

      # original response (as character vector)
        resp_gentrust_original = NA,       

      # binary recode (numeric: 1 = any negative attitude expressed; 0 otherwise)
        resp_gentrust_bin_recode = NA,    
    
    #########################  
    #### RELIGIOSITY ########  
    #########################
    
      # original question number; question text; response options (input above)
        resp_religiosity_qinfo = "NUM: attend; QTEXT: Aside from weddings and funerals, how often do you attend religious services?; ROPTIONS: 1 = More than once a week + 2 = Once a week + 3 = Once or twice a month + 4 = A few times a year + 5 = Seldom + 6 = Never",

      # original response (as numeric vector, with non-substantive responses coded as NA_real_)
        resp_religiosity_original = 
          dplyr::recode(
            as.numeric(attend),
            `9` = NA_real_),

      # recode (numeric: scaled 0-1, where 1 is more religious)
        resp_religiosity_recode = (6 - resp_religiosity_original)/5,
    
    ) %>% 
    select(starts_with("resp_"))
```

# Clean Pew Global Attitudes Spring 2005 - 11 Countries

```{r}
# declare dates
  dates_pew_gap_spring_05_11couintries <-
    tribble(
       ~resp_country_common, ~resp_interview_start_date, ~resp_interview_end_date,
       "UK", "2005-04-25", "2005-05-07",
       "Canada", "2005-05-06", "2005-05-11",
       "China", "2005-05-21", "2005-05-31",
       "France", "2005-05-02", "2005-05-07",
       "Germany", "2005-04-27", "2005-05-04",
       "India", "2005-05-01", "2005-05-29",
       "Netherlands", "2005-04-27", "2005-05-11",
       "Poland", "2005-04-27", "2005-05-29",
       "Russia", "2005-04-28", "2005-05-13",
       "Spain", "2005-04-20", "2005-04-28",
       "US", "2005-05-18", "2005-05-22"
    ) %>%
    mutate(
        resp_interview_start_date = as.Date(resp_interview_start_date, "%Y-%m-%d"),
        resp_interview_end_date = as.Date(resp_interview_end_date, "%Y-%m-%d"))

# clean pew global attitudes spring 2005 - 11 countries
  n2_clean_pew_gap_spring_05_11countries <-
    pew_gap_spring_05_11countries_raw %>% 
    mutate(
      
    #########################  
    ####### META-DATA #######  
    #########################      
      
      # source name (character vector, title case)
        resp_source = "Pew Global Attitudes Project",
        
      # round number (character vector, title case)  
        resp_round = "Spring 2005",
      
      # url to dataset source, where publicly available (character vector)
        resp_original_data_url = "pewrsr.ch/3wToJeP",

      # country (character vector; list of countries as written in original source)
        resp_country_original = to_character(country),
    
      # survey mode (in-person/phone/internet/mail)
        resp_survey_mode = 
          case_when(
            resp_country_original %in% c("China", "India", "Poland", "Russia") ~ "in-person",
            resp_country_original %in% c("Britain", "Canada", "France", "Germany", "Netherlands", "Spain", "US") ~ "phone"),

      # country (character vector; converts to countrycode county.name list)
        resp_country_common = 
          countryname(resp_country_original),
        
      # interview date (variable of class Date; if only month given, input 1st of month)
        resp_interview_date = as.Date(as.character(D13), "%d%m%Y")) %>%
    left_join(dates_pew_gap_spring_05_11couintries, by = "resp_country_common") %>%
    mutate(
   
    #########################  
    ##### DEMOGRAPHICS ######  
    #########################
      
      # respondent denomination (character vector, recoded jointly below)
        across(starts_with("d3"), to_character)) %>% 
    mutate(
        resp_denomination = coalesce(!!! select(., starts_with("d3"))),
      
      # respondent's age (character vector; bins denoted by single dash ["18-25"])
        resp_age =
          dplyr::recode(
            as.character(D2),
            "97" = "97+",
            "98" = NA_character_,
            "99" = NA_character_),        
      
      # respondent's education level
        resp_education_original =
          case_when(
            #France
            D5FRA == 1 ~ "1. No Formal Education [No education]",
            D5FRA == 2 ~ "2. Incomplete primary education [No education]",
            D5FRA == 3 ~ "3. Completed primary education [Primary]",
            D5FRA == 4 ~ "4. Vocational education, but no qualifications [Primary]",
            D5FRA == 5 ~ "5. Vocational education, with qualifications [Primary]", # not full university education
            D5FRA == 6 ~ "6. Secondary education but no qualifications [Primary]",
            D5FRA == 7 ~ "7. Secondary education with qualifications [Primary]",
            D5FRA == 8 ~ "8. Higher education but no qualifications [Primary]",
            D5FRA == 9 ~ "9. Complete higher education with qualifications [College]",
            D5FRA == 98 ~ NA_character_,
            D5FRA == 99 ~ NA_character_,
            #Germany
            D5GER == 1 ~ "1. still attending school [Primary]",
            D5GER == 2 ~ "2. lower secondary school without professional training [Primary]",
            D5GER == 3 ~ "3. lower secondary school with professional training [Primary]",
            D5GER == 4 ~ "4. middle secondary school without matriculation standard [Primary]",
            D5GER == 5 ~ "5. general qualifications for university entrance [Primary]",
            D5GER == 6 ~ "6. university degree [College]",
            D5GER == 8 ~ NA_character_,
            D5GER == 9 ~ NA_character_,
            #Spain
            d5spa == 1 ~ "1. No formal education [No education]",
            d5spa == 2 ~ "2. Primary studies incomplete [No education]",
            d5spa == 3 ~ "3. Primary studies complete (EGB 1) [Primary]",
            d5spa == 4 ~ "4. Secondary studies - stage 1 (EGB 2) [Primary]",
            d5spa == 5 ~ "5. Secondary studies - stage 2 [Primary]",
            d5spa == 6 ~ "6. Tertiary studies - college [College]", # assume first stage
            d5spa == 7 ~ "7. Tertiary studies - university [College]",
            d5spa == 8 ~ NA_character_,
            d5spa == 9 ~ NA_character_,
            #Britain
            D5BRI == 1 ~ "1. No qualifications obtained [Primary]", # since primary compulsory
            D5BRI == 2 ~ "2. GCSE or 'O' level grades D or below / CSE grades 2 or below [Primary]",
            D5BRI == 3 ~ "3. GCSE or 'O' level grades A-C / CSE grade 1/ NVQ 2 [Primary]",
            D5BRI == 4 ~ "4. 'A' level / NVQ 3 [Primary]",
            D5BRI == 5 ~ "5. Degree / HND / teaching or nursing certificates / NVQ 4 [College]",
            D5BRI == 6 ~ "6. Higher degree (masters, PHD, NVQ 5) [College]",
            D5BRI == 7 ~ "7. Other qualification [Primary]",
            D5BRI == 8 ~ NA_character_,
            D5BRI == 9 ~ NA_character_,
            D5BRI == 10 ~ NA_character_,
            #Netherlands
            d5net == 1 ~ "1. No schooling [No education]",
            d5net == 2 ~ "2. Lower school [Primary]",
            d5net == 3 ~ "3. Middle school [Primary]",
            d5net == 4 ~ "4. Intermediate Vocational training (MBO) [Primary]",
            d5net == 5 ~ "5. Higher Vocational training (MBO) [Primary]",
            d5net == 6 ~ "6. University [College]",
            d5net == 7 ~ "7. Ph.D. [College]",
            d5net == 8 ~ "8. Other, namely (specify) [Primary]",
            d5net == 98 ~ NA_character_,
            d5net == 99 ~ NA_character_,
            #Poland
            D5POL == 1 ~ "1. No formal education [No education]",
            D5POL == 2 ~ "2. Incomplete primary school [No education]",
            D5POL == 3 ~ "3. Complete primary school [Primary]",
            D5POL == 4 ~ "4. Vocational school (2-3 years) [Primary]",
            D5POL == 5 ~ "5. Complete secondary school [Primary]",
            D5POL == 6 ~ "6. Incomplete post secondary school [Primary]",
            D5POL == 7 ~ "7. Complete post secondary school [Primary]",
            D5POL == 8 ~ "8. Some university-level education, without a diploma [Primary]",
            D5POL == 9 ~ "9. University-level education, with diploma [College]",
            D5POL == 99 ~ NA_character_,
            #Russia
            d5rus == 1 ~ "1. No official education [No education]",
            d5rus == 2 ~ "2. Incomplete primary school [No education]",
            d5rus == 3 ~ "3. Complete primary school [Primary]",
            d5rus == 4 ~ "4. Incomplete secondary education: technical college, Professio [Primary]",
            d5rus == 5 ~ "5. Completed secondary education: technical school, Professiona [Primary]",
            d5rus == 6 ~ "6. Incomplete higher education [Primary]",
            d5rus == 7 ~ "7. Complete higher education [College]",
            d5rus == 98 ~ NA_character_,
            d5rus == 99 ~ NA_character_,
            #India
            D5IDA == 1 ~ "1. Illiterate [No education]",
            D5IDA == 2 ~ "2. Literate but no formal schooling [No education]",
            D5IDA == 3 ~ "3. School up to 4 years [No education]",
            D5IDA == 4 ~ "4. School up to 5 to 9 years [Primary]",
            D5IDA == 5 ~ "5. SSC/HSC [Primary]",
            D5IDA == 6 ~ "6. Some college but not graduated [Primary]",
            D5IDA == 7 ~ "7. Graduate/Post grad-Gen BA MSc BCom etc [College]",
            D5IDA == 8 ~ "8. Graduate/Post grad-Prof BE MTech MBA MBBS etc [College]",
            D5IDA == 98 ~ NA_character_,
            D5IDA == 99 ~ NA_character_,
            #China
            D5CHI == 1 ~ "1. No formal education [No education]",
            D5CHI == 2 ~ "2. Primary school [Primary]",
            D5CHI == 3 ~ "3. Junior Middle [Primary]",
            D5CHI == 4 ~ "4. Senior middle/ technical school [Primary]",
            D5CHI == 5 ~ "5. College/university [Primary]", #assume given next entry
            D5CHI == 6 ~ "6. Bachelor [College]",
            D5CHI == 7 ~ "7. Master [College]",
            D5CHI == 8 ~ "8. Doctor [College]",
            D5CHI == 98 ~ NA_character_,
            D5CHI == 99 ~ NA_character_,
            #United States
            d5us == 1 ~ "1. None, or grade 1-8 [Primary]",
            d5us == 2 ~ "2. High school incomplete (Grades 9-11) [Primary]",
            d5us == 3 ~ "3. High school graduate (Grade 12 or GED certificate) [Primary]",
            d5us == 4 ~ "4. Business, Technical, or vocational school AFTER high school [Primary]",
            d5us == 5 ~ "5. Some college, no 4-year degree [Primary]",
            d5us == 6 ~ "6. College graduate (B.S., B.A., or other 4-year degree) [College]",
            d5us == 7 ~ "7. Post-grad training or professional schooling after college [College]",
            d5us == 8 ~ NA_character_,
            d5us == 9 ~ NA_character_,
            #Canada
            d5can == 1 ~ "1. Some elementary [No education]",
            d5can == 2 ~ "2. Completed elementary [Primary]",
            d5can == 3 ~ "3. Some high school [Primary]",
            d5can == 4 ~ "4. Completed high school [Primary]",
            d5can == 5 ~ "5. Community college/vocational/trade school/commercial/CEGEP [Primary]",
            d5can == 6 ~ "6. Some university [Primary]",
            d5can == 7 ~ "7. Completed university [College]",
            d5can == 8 ~ "8. Post-graduate university/professional school [College]",
            d5can == 98 ~ NA_character_,
            d5can == 99 ~ NA_character_),       
      
      # respondent's gender (numeric: female = 1; male = 0; other = NA)
        resp_female =
          case_when(
            D1 == 1 ~ 0,
            D1 == 2 ~ 1),
      
      # respondent resident in rural (vs urban) area (numeric: rural = 1; urban/semi-urban/peri-urban = 0)
        resp_rural =  # split across many variables, not all countries covered
          case_when(
            d14place == 1 ~ 1,
            d14place %in% c(2:4) ~ 0,
            
            usr == "R" ~ 1,
            usr %in% c("S", "U") ~ 0,

            type == 1 ~ 1,
            type %in% c(2:7) ~ 0,
            
            gs == 1 ~ 0,
            gs == 2 ~ 1,

            urbrur == 1 ~ 0,
            urbrur == 2 ~ 1,
            TRUE ~ NA_real_), # note also density3, but insufficient information
      
    #########################  
    ### SOCIAL DISTANCE 1 ###  
    #########################
    
      # original question number; question text; response options (input above)
        resp_soc_dist_1_qinfo = "NUM: Q5 [Q5g]; QTEXT: Please tell me if you have a very favorable, somewhat favorable, somewhat unfavorable or very unfavorable opinion of Jews?; ROPTIONS: 1 = Very favorable [=0] + 2 = Somewhat favorable [=0] + 3 = Somewhat unfavorable [=1] + 4 = Very unfavorable [=1]; TARGET: Jewish; TYPE: Favorability",
      
      # original response (as character vector)
        resp_soc_dist_1_original = 
          dplyr::recode(
            as.character(Q5g),
            "8" = NA_character_,
            "9" = NA_character_),       

      # binary recode (numeric: 1 = any negative attitude expressed; 0 otherwise)
        resp_soc_dist_1_bin_recode = 
          case_when(
            Q5g %in% c(1:2) ~ 0,
            Q5g %in% c(3:4) ~ 1,
            TRUE ~ NA_real_),
    
    #########################  
    ### SOCIAL DISTANCE 2 ###  
    #########################
    
      # original question number; question text; response options (input above)
        resp_soc_dist_2_qinfo = "NUM: Q5 [Q5h]; QTEXT: Please tell me if you have a very favorable, somewhat favorable, somewhat unfavorable or very unfavorable opinion of Christians?; ROPTIONS: 1 = Very favorable [=0] + 2 = Somewhat favorable [=0] + 3 = Somewhat unfavorable [=1] + 4 = Very unfavorable [=1]; TARGET: Christian; TYPE: Favorability",
      
      # original response (as character vector)
        resp_soc_dist_2_original = 
          dplyr::recode(
            as.character(Q5h),
            "8" = NA_character_,
            "9" = NA_character_),       

      # binary recode (numeric: 1 = any negative attitude expressed; 0 otherwise)
        resp_soc_dist_2_bin_recode = 
          case_when(
            Q5h %in% c(1:2) ~ 0,
            Q5h %in% c(3:4) ~ 1,
            TRUE ~ NA_real_),
    
    #########################  
    ### SOCIAL DISTANCE 3 ###  
    #########################
    
      # original question number; question text; response options (input above)
        resp_soc_dist_3_qinfo = "NUM: Q5 [Q5i]; QTEXT: Please tell me if you have a very favorable, somewhat favorable, somewhat unfavorable or very unfavorable opinion of Muslims?; ROPTIONS: 1 = Very favorable [=0] + 2 = Somewhat favorable [=0] + 3 = Somewhat unfavorable [=1] + 4 = Very unfavorable [=1]; TARGET: Muslim; TYPE: Favorability",
      
      # original response (as character vector)
        resp_soc_dist_3_original = 
          dplyr::recode(
            as.character(Q5i),
            "8" = NA_character_,
            "9" = NA_character_),       

      # binary recode (numeric: 1 = any negative attitude expressed; 0 otherwise)
        resp_soc_dist_3_bin_recode = 
          case_when(
            Q5i %in% c(1:2) ~ 0,
            Q5i %in% c(3:4) ~ 1,
            TRUE ~ NA_real_),
    
    ############################  
    ### GENERAL SOCIAL TRUST ###  
    ############################
    
      # original question number; question text; response options (input above)
        resp_gentrust_qinfo = NA, # checked; no question asked

      # original response (as character vector)
        resp_gentrust_original = NA,       

      # binary recode (numeric: 1 = any negative attitude expressed; 0 otherwise)
        resp_gentrust_bin_recode = NA,    
    
    #########################  
    ##### RELIGIOSITY #######  
    #########################
    
      # original question number; question text; response options (input above)
        resp_religiosity_qinfo = "NUM: D4 [D4]; QTEXT: How important is religion in your life – very important, somewhat important, not too important, or not at all important?; ROPTIONS: 1 = Very important + 2 = Somewhat important + 3 = Not too important + 4 = Not at all important",
  
      # original response (as numeric vector, with non-substantive responses coded as NA_real_)
        resp_religiosity_original = 
          dplyr::recode(
            as.numeric(D4),
            `8` = NA_real_,
            `9` = NA_real_), # note, we ignore D4CHI (religiosity for China), as it is on a different scale and no elections in that country
    
      # recode (numeric: scaled 0-1, where 1 is more religious)
        resp_religiosity_recode = (4 - resp_religiosity_original)/3
    
    ) %>% 
    select(starts_with("resp_"))
```

# Clean Pew Global Attitudes Spring 2005 - 6 Countries

```{r}
# declare dates
  dates_pew_gap_spring_05_6couintries <-
    tribble(
       ~resp_country_common, ~resp_interview_start_date, ~resp_interview_end_date,
       "Indonesia", "2005-04-30", "2005-05-16",
       "Jordan", "2005-05-03", "2005-05-24",
       "Lebanon", "2005-05-03", "2005-05-24",
       "Morocco", "2005-06-06", "2005-06-16",
       "Pakistan", "2005-05-02", "2005-05-24",
       "Turkey", "2005-04-27", "2005-05-14"
    ) %>%
    mutate(
        resp_interview_start_date = as.Date(resp_interview_start_date, "%Y-%m-%d"),
        resp_interview_end_date = as.Date(resp_interview_end_date, "%Y-%m-%d"))

# clean pew global attitudes spring 2005 - 6 countries
  n3_clean_pew_gap_spring_05_6countries <-
    pew_gap_spring_05_6countries_raw %>% 
    mutate(
      
    #########################  
    ####### META-DATA #######  
    #########################      
      
      # source name (character vector, title case)
        resp_source = "Pew Global Attitudes Project",
        
      # round number (character vector, title case)  
        resp_round = "Spring 2005",
      
      # url to dataset source, where publicly available (character vector)
        resp_original_data_url = "pewrsr.ch/3wToJeP",

      # country (character vector; list of countries as written in original source)
        resp_country_original = to_character(country),
    
    # survey mode (in-person/phone/internet/mail)
        resp_survey_mode = "in-person", # confirmed

      # country (character vector; converts to countrycode county.name list)
        resp_country_common = 
          countryname(resp_country_original),
        
      # interview date (variable of class Date; if only month given, input 1st of month)
        resp_interview_date = as.Date(as.character(D13), "%d%m%Y")) %>%
    left_join(dates_pew_gap_spring_05_6couintries, by = "resp_country_common") %>%
    mutate(
   
    #########################  
    ##### DEMOGRAPHICS ######  
    #########################

    # respondent denomination (character vector, recoded jointly below)
        across(starts_with("q16"), to_character)) %>% 
    mutate(
        resp_denomination = coalesce(!!! select(., starts_with("q16"))),

      # respondent's age (character vector; bins denoted by single dash ["18-25"])
        resp_age =
          dplyr::recode(
            as.character(D2),
            "97" = "97+",
            "98" = NA_character_,
            "99" = NA_character_),         
      
      # respondent's education level
        resp_education_original =
          case_when(
            #Turkey
            D5TUR == 1 ~ "1. No formal education [No education]",
            D5TUR == 2 ~ "2. Incomplete primary school [No education]",
            D5TUR == 3 ~ "3. Complete primary school [Primary]",
            D5TUR == 4 ~ "4. Incomplete secondary school: technical/vocational type [Primary]",
            D5TUR == 5 ~ "5. Complete secondary school: technical/vocational type [Primary]",
            D5TUR == 6 ~ "6. Incomplete secondary: university-preparatory type [Primary]",
            D5TUR == 7 ~ "7. Complete secondary: university-preparatory type [Primary]",
            D5TUR == 8 ~ "8. Some university-level education, without a degree [Primary]",
            D5TUR == 9 ~ "9. University-level education, with degree [College]",
            D5TUR == 98 ~ NA_character_,
            D5TUR == 99 ~ NA_character_,
            #Pakistan
            d5PAK == 1 ~ "1. No formal education [No education]",
            d5PAK == 2 ~ "2. No formal education but can read and write [No education]",
            d5PAK == 3 ~ "3. Less than 5 classes [No education]",
            d5PAK == 4 ~ "4. 5-9 classes [Primary]",
            d5PAK == 5 ~ "5. Matric [Primary]",
            d5PAK == 6 ~ "6. Intermediate [Primary]",
            d5PAK == 7 ~ "7. Graduate [College]",
            d5PAK == 8 ~ "8. Post-graduate [College]",
            d5PAK == 98 ~ NA_character_,
            d5PAK == 99 ~ NA_character_,
            #Indonesia
            D5IND == 1 ~ "1. No formal education [No education]",
            D5IND == 2 ~ "2. Incomplete primary school [No education]",
            D5IND == 3 ~ "3. Complete primary school [Primary]",
            D5IND == 4 ~ "4. Incomplete secondary school: technical/vocational type [Primary]",
            D5IND == 5 ~ "5. Complete secondary school: technical/vocational type [Primary]",
            D5IND == 6 ~ "6. Incomplete secondary: university-preparatory type [Primary]",
            D5IND == 7 ~ "7. Complete Secondary University-preparatory type [Primary]",
            D5IND == 8 ~ "8. University-level education, without degree [Primary]",
            D5IND == 9 ~ "9. University-level education, with degree [College]",
            D5IND == 98 ~ NA_character_,
            D5IND == 99 ~ NA_character_,
            #Lebanon
            d5leb == 1 ~ "1. No formal education [No education]",
            d5leb == 2 ~ "2. Incomplete primary [No education]",
            d5leb == 3 ~ "3. Complete primary [Primary]",
            d5leb == 4 ~ "4. Incomplete secondary (vocational) [Primary]",
            d5leb == 5 ~ "5. Complete secondary (vocational) [Primary]",
            d5leb == 6 ~ "6. Incomplete secondary (preparatory) [Primary]",
            d5leb == 7 ~ "7. Complete secondary (preparatory) [Primary]",
            d5leb == 8 ~ "8. Some university without degree [Primary]",
            d5leb == 9 ~ "9. University with degree [College]",
            d5leb == 98 ~ NA_character_,
            d5leb == 99 ~ NA_character_,
            #Jordan
            d5jor == 1 ~ "1. No formal education [No education]",
            d5jor == 2 ~ "2. Incomplete primary [No education]",
            d5jor == 3 ~ "3. Complete primary [Primary]",
            d5jor == 4 ~ "4. Incomplete secondary (vocational) [Primary]",
            d5jor == 5 ~ "5. Complete secondary (vocational) [Primary]",
            d5jor == 6 ~ "6. Incomplete secondary (preparatory) [Primary]",
            d5jor == 7 ~ "7. Complete secondary (preparatory) [Primary]",
            d5jor == 8 ~ "8. Some university without degree [Primary]",
            d5jor == 9 ~ "9. University with degree [College]",
            d5jor == 98 ~ NA_character_,
            d5jor == 99 ~ NA_character_,
            #Morocco
            d5mor == 1 ~ "1. No formal education [No education]",
            d5mor == 2 ~ "2. Incomplete primary school [No education]",
            d5mor == 3 ~ "3. Complete primary school [Primary]",
            d5mor == 4 ~ "4. Incomplete secondary school: technical/vocational type [Primary]",
            d5mor == 5 ~ "5. Complete secondary school: technical/vocational type [Primary]",
            d5mor == 6 ~ "6. Incomplete secondary: university-preparatory type [Primary]",
            d5mor == 7 ~ "7. Complete secondary: university-preparatory type [Primary]",
            d5mor == 8 ~ "8. University-level education, without degree [Primary]",
            d5mor == 9 ~ "9. University-level education, with degree [College]",
            d5mor == 98 ~ NA_character_,
            d5mor == 99 ~ NA_character_,
          ),       
      
      # respondent's gender (numeric: female = 1; male = 0; other = NA)
        resp_female =
          case_when(
            D1 == 1 ~ 0,
            D1 == 2 ~ 1),
      
      # respondent resident in rural (vs urban) area (numeric: rural = 1; urban/semi-urban/peri-urban = 0)
        resp_rural =
          case_when(
            urb_rurl == 1 ~ 0,
            urb_rurl == 2 ~ 1,
            
            UR == 1 ~ 0,
            UR == 2 ~ 1,
            
            TRUE ~ NA_real_),
      
    #########################  
    ### SOCIAL DISTANCE 1 ###  
    #########################
    
      # original question number; question text; response options (input above)
        resp_soc_dist_1_qinfo = "NUM: Q5 [Q5g]; QTEXT: Please tell me if you have a very favorable, somewhat favorable, somewhat unfavorable or very unfavorable opinion of Jews?; ROPTIONS: 1 = Very favorable [=0] + 2 = Somewhat favorable [=0] + 3 = Somewhat unfavorable [=1] + 4 = Very unfavorable [=1]; TARGET: Jewish; TYPE: Favorability",
      
      # original response (as character vector)
        resp_soc_dist_1_original = 
          dplyr::recode(
            as.character(Q5g),
            "8" = NA_character_,
            "9" = NA_character_),       

      # binary recode (numeric: 1 = any negative attitude expressed; 0 otherwise)
        resp_soc_dist_1_bin_recode = 
          case_when(
            Q5g %in% c(1:2) ~ 0,
            Q5g %in% c(3:4) ~ 1,
            TRUE ~ NA_real_),
    
    #########################  
    ### SOCIAL DISTANCE 2 ###  
    #########################
    
      # original question number; question text; response options (input above)
        resp_soc_dist_2_qinfo = "NUM: Q5 [Q5h]; QTEXT: Please tell me if you have a very favorable, somewhat favorable, somewhat unfavorable or very unfavorable opinion of Christians?; ROPTIONS: 1 = Very favorable [=0] + 2 = Somewhat favorable [=0] + 3 = Somewhat unfavorable [=1] + 4 = Very unfavorable [=1]; TARGET: Christian; TYPE: Favorability",
      
      # original response (as character vector)
        resp_soc_dist_2_original = 
          dplyr::recode(
            as.character(Q5h),
            "8" = NA_character_,
            "9" = NA_character_),       

      # binary recode (numeric: 1 = any negative attitude expressed; 0 otherwise)
        resp_soc_dist_2_bin_recode = 
          case_when(
            Q5h %in% c(1:2) ~ 0,
            Q5h %in% c(3:4) ~ 1,
            TRUE ~ NA_real_),
    
    #########################  
    ### SOCIAL DISTANCE 3 ###  
    #########################
    
      # original question number; question text; response options (input above)
        resp_soc_dist_3_qinfo = "NUM: Q5 [Q5i]; QTEXT: Please tell me if you have a very favorable, somewhat favorable, somewhat unfavorable or very unfavorable opinion of Muslims?; ROPTIONS: 1 = Very favorable [=0] + 2 = Somewhat favorable [=0] + 3 = Somewhat unfavorable [=1] + 4 = Very unfavorable [=1]; TARGET: Muslim; TYPE: Favorability",
      
      # original response (as character vector)
        resp_soc_dist_3_original = 
          dplyr::recode(
            as.character(Q5i),
            "8" = NA_character_,
            "9" = NA_character_),       

      # binary recode (numeric: 1 = any negative attitude expressed; 0 otherwise)
        resp_soc_dist_3_bin_recode = 
          case_when(
            Q5i %in% c(1:2) ~ 0,
            Q5i %in% c(3:4) ~ 1,
            TRUE ~ NA_real_),
    
    ############################  
    ### GENERAL SOCIAL TRUST ###  
    ############################
    
      # original question number; question text; response options (input above)
        resp_gentrust_qinfo = NA, # checked; no question asked

      # original response (as character vector)
        resp_gentrust_original = NA,       

      # binary recode (numeric: 1 = any negative attitude expressed; 0 otherwise)
        resp_gentrust_bin_recode = NA,    
    
    #########################  
    ##### RELIGIOSITY #######  
    #########################
    
      # original question number; question text; response options (input above)
        resp_religiosity_qinfo = "NUM: D4; QTEXT: How important is religion in your life – very important, somewhat important, not too important, or not at all important?; ROPTIONS: 1 = Very important + 2 = Somewhat important + 3 = Not too important + 4 = Not at all important",
  
      # original response (as numeric vector, with non-substantive responses coded as NA_real_)
        resp_religiosity_original = 
          dplyr::recode(
            as.numeric(D4),
            `8` = NA_real_,
            `9` = NA_real_),       

      # recode (numeric: scaled 0-1, where 1 is more religious)
        resp_religiosity_recode = (4 - resp_religiosity_original)/3
    
    ) %>% 
    select(starts_with("resp_"))
```

# Clean Pew Global Attitudes Spring 2006

```{r}
# declare dates
  dates_pew_gap_spring_06 <-
    tribble(
        ~resp_country_common, ~resp_interview_start_date, ~resp_interview_end_date,
        "UK", "April 4, 2006", "April 26, 2006",
        "France", "April 5, 2006", "April 19, 2006",
        "Germany", "April 5, 2006", "April 27, 2006",
        "Spain", "April 7, 2006", "May 4, 2006",
        "United States", "May 2, 2006", "May 14, 2006",
        "Russia", "April 6, 2006", "April 16, 2006",
        "Nigeria", "April 20, 2006", "April 29, 2006",
        "China", "April 7, 2006", "April 18, 2006",
        "Japan", "March 31, 2006", "April 21, 2006",
        "India", "April 15, 2006", "May 3, 2006",
        "Turkey", "April 1, 2006", "April 25, 2006",
        "Indonesia", "April 8, 2006", "April 30, 2006",
        "Pakistan", "April 7, 2006", "April 28, 2006",
        "Jordan", "April 5, 2006", "April 27, 2006",
        "Egypt", "April 5, 2006", "April 27, 2006"
      ) %>%
      mutate(
        resp_interview_start_date = as.Date(resp_interview_start_date, "%B %d, %Y"),
        resp_interview_end_date = as.Date(resp_interview_end_date, "%B %d, %Y"))
  
# clean pew global attitudes
  n14_clean_pew_gap_spring_06 <-
    pew_gap_spring_06_raw %>% 
    mutate(
      
    #########################  
    ####### META-DATA #######  
    #########################      
      
      # source name (character vector, title case)
        resp_source = "Pew Global Attitudes Project",
        
      # round number (character vector, title case)  
        resp_round = "Spring 2006",
      
      # url to dataset source, where publicly available (character vector)
        resp_original_data_url = "pewrsr.ch/3wToJeP",

      # survey mode (in-person/phone/internet/mail)
        resp_survey_mode =
          case_when(
            country %in% c(1, 4, 5, 8, 15) ~ "phone",
            TRUE ~ "in-person"),

      # country (character vector; list of countries as written in original source)
        resp_country_original = to_character(country),

      # country (character vector; converts to countrycode county.name list)
        resp_country_common = 
          countryname(resp_country_original),
        
      # interview date (variable of class Date; if only month given, input 1st of month)
        # paste year 2006 to date variable
        temp_interview_date = paste0("20060", as.character(Date_Interview)), 
        resp_interview_date = as.Date(temp_interview_date, "%Y%m%d")) %>%
    left_join(dates_pew_gap_spring_06, by = "resp_country_common") %>%
   
    #########################  
    ##### DEMOGRAPHICS ######  
    #########################

    # **RELIGION NOT CODED**
      # respondent denomination (recoded jointly below)
    dplyr::rename(importance_of_religion = Religion_Import) %>% 
    mutate(
  
  # respondent denomination (character vector, recoded jointly below)
        across(starts_with("Religion"), to_character)) %>% 
    mutate(
        resp_denomination = coalesce(!!! select(., starts_with("Religion"))),        

      # respondent's age (character vector; bins denoted by single dash ["18-25"])
        resp_age =
          dplyr::recode(
            as.character(Age),
            "97" = "97+",
            "98" = NA_character_,
            "99" = NA_character_), 
    
      # respondent's education level
        resp_education_original =
          case_when(
            # Britain
            EducationBRI == 1 ~ "1. No qualifications obtained [Primary]", #since primary compulsory
            EducationBRI == 2 ~ "2. GCSE or O level grades D or below / CSE grades 2 or below [Primary]",
            EducationBRI == 3 ~ "3. GCSE or O level grades A-C / CSE grade 1/ NVQ 2 [Primary]",
            EducationBRI == 4 ~ "4. A level / NVQ 3 [Primary]",
            EducationBRI == 5 ~ "5. Degree / HND / teaching or nursing certificates / NVQ 4 [College]",
            EducationBRI == 6 ~ "6. Higher degree (masters, PHD, NVQ 5) [College]",
            EducationBRI == 7 ~ "7. Other qualification [Primary]",
            EducationBRI == 8 ~ NA_character_,
            EducationBRI == 9 ~ NA_character_,
            # China
            EducationCHI == 1 ~ "1. Below primary school [No education]",
            EducationCHI == 2 ~ "2. Primary school [Primary]",
            EducationCHI == 3 ~ "3. Junior school [Primary]",
            EducationCHI == 4 ~ "4. High school/technical secondary school [Primary]",
            EducationCHI == 5 ~ "5. College [Primary]",
            EducationCHI == 6 ~ "6. Bachelor's degree [College]",
            EducationCHI == 7 ~ "7. Master’s degree [College]",
            EducationCHI == 8 ~ "8. Doctor’s degree and above [College]",
            EducationCHI %in% c(97, 98, 99) ~ NA_character_,
            # Egypt
            EducationEGY == 1 ~ "1. No formal education [No education]",
            EducationEGY == 2 ~ "2. Illiterate [No education]",
            EducationEGY == 3 ~ "3. Completed elementary [Primary]",
            EducationEGY == 4 ~ "4. Did not complete intermediate [Primary]",
            EducationEGY == 5 ~ "5. Did not complete secondary [Primary]",
            EducationEGY == 6 ~ "6. Completed secondary [Primary]",
            EducationEGY == 7 ~ "7. Completed college [Primary]", # assume this does not mean bachelors
            EducationEGY == 8 ~ "8. Completed university in Middle East/Africa [College]",
            EducationEGY == 9 ~ "9. Completed university in Western Europe/America [College]",
            EducationEGY == 10 ~ "10. Completed Masters/Post graduate/Doctorate [College]",
            EducationEGY %in% c(98, 99) ~ NA_character_,
            # France
            EducationFRA == 1 ~ "1. No formal education [No education]",
            EducationFRA == 2 ~ "2. Primary school [Primary]",
            EducationFRA == 3 ~ "3. Secondary school: technical/vocational type [Primary]",
            EducationFRA == 4 ~ "4. Secondary school: university-preparatory type [Primary]",
            EducationFRA == 5 ~ "5. Some university-level education, without degree [Primary]",
            EducationFRA == 6 ~ "6. University-level education, with degree [College]",
            EducationFRA %in% c(8, 9) ~ NA_character_,
            # Germany
            EducationGER == 1 ~ "1. Still attending school [Primary]",
            EducationGER == 2 ~ "2. Lower secondary school without professional training [Primary]",
            EducationGER == 3 ~ "3. lower secondary school with professional training [Primary]",
            EducationGER == 4 ~ "4. middle secondary school without matriculation standard [Primary]",
            EducationGER == 5 ~ "5. general qualifications for university entrance [Primary]",
            EducationGER == 6 ~ "6. university degree [College]",
            EducationGER %in% c(8, 9) ~ NA_character_,
            # India
            EducationINDIA == 1 ~ "1. Illiterate [No education]",
            EducationINDIA == 2 ~ "2. Literate but no formal schooling [No education]",
            EducationINDIA == 3 ~ "3. School up to 4 years [No education]",
            EducationINDIA == 4 ~ "4. School up to 5 to 9 years [Primary]",
            EducationINDIA == 5 ~ "5. SSC/HSC [Primary]",
            EducationINDIA == 6 ~ "6. Some college but not graduated [Primary]",
            EducationINDIA == 7 ~ "7. Graduate/Post grad-Gen BA MSc BCom etc [College]",
            EducationINDIA == 8 ~ "8. Graduate/Post grad-Prof BE MTech MBA MBBS etc [College]", #since this includes post-grad technical degrees
            EducationINDIA %in% c(98, 99) ~ NA_character_,
            # Indonesia [wrong]
            EducationINDO == 1 ~ "1. No formal education [No education]",
            EducationINDO == 2 ~ "2. Incomplete primary school [No education]",
            EducationINDO == 3 ~ "3. Complete primary school [Primary]",
            EducationINDO == 4 ~ "4. Incomplete secondary school technical/vocational type [Primary]",
            EducationINDO == 5 ~ "5. Complete secondary school technical/vocational type [Primary]",
            EducationINDO == 6 ~ "6. Incomplete secondary University-preparatory type [Primary]",
            EducationINDO == 7 ~ "7. Complete Secondary University-preparatory type [Primary]",
            EducationINDO == 8 ~ "8. University-level education, without degree [Primary]",
            EducationINDO == 9 ~ "9. University-level education, with degree [College]",
            EducationINDO %in% c(98, 99) ~ NA_character_, 
            # Jordan
            EducationJOR == 1 ~ "1. No formal education [No education]",
            EducationJOR == 2 ~ "2. Incomplete primary [No education]",
            EducationJOR == 3 ~ "3. Complete primary [Primary]",
            EducationJOR == 4 ~ "4. Incomplete secondary (vocational) [Primary]",
            EducationJOR == 5 ~ "5. Complete secondary (vocational) [Primary]",
            EducationJOR == 6 ~ "6. Incomplete secondary (preparatory) [Primary]",
            EducationJOR == 7 ~ "7. Complete secondary (preparatory) [Primary]",
            EducationJOR == 8 ~ "8. Some university without degree [Primary]",
            EducationJOR == 9 ~ "9. University with degree [College]",
            EducationJOR %in% c(98, 99) ~ NA_character_, 
            # Nigeria
            EducationNIG == 1 ~ "1. No formal education [No education]",
            EducationNIG == 2 ~ "2. Incomplete primary school [No education]",
            EducationNIG == 3 ~ "3. Complete primary school [Primary]",
            EducationNIG == 4 ~ "4. Incomplete secondary school: technical/vocational type [Primary]",
            EducationNIG == 5 ~ "5. Complete secondary school: technical/vocational type [Primary]",
            EducationNIG == 6 ~ "6. Incomplete secondary: university-preparatory type [Primary]",
            EducationNIG == 7 ~ "7. Complete secondary: university-preparatory type [Primary]",
            EducationNIG == 8 ~ "8. Incomplete College of Education [Primary]",
            EducationNIG == 9 ~ "9. Complete College of Education [Primary]",
            EducationNIG == 10 ~ "10. Incomplete Polytechnic [Primary]",
            EducationNIG == 11 ~ "11. Complete Polytechnic [Primary]",
            EducationNIG == 12 ~ "12. Some university-level education, without a degree [Primary]",
            EducationNIG == 13 ~ "13. University-level education, with degree [College]",
            EducationNIG %in% c(14, 15) ~ NA_character_,
            # Pakistan
            EducationPAK == 1 ~ "1. No formal education [No education]",
            EducationPAK == 2 ~ "2. No formal education but can read and write [No education]",
            EducationPAK == 3 ~ "3. Less than 5 classes [No education]",
            EducationPAK == 4 ~ "4. 5-9 classes [Primary]",
            EducationPAK == 5 ~ "5. Matric [Primary]",
            EducationPAK == 6 ~ "6. Intermediate [Primary]",
            EducationPAK == 7 ~ "7. Graduate [College]",
            EducationPAK == 8 ~ "8. Post-graduate [College]",
            EducationPAK %in% c(98, 99) ~ NA_character_,
            # Russia
            EducationRUS == 1 ~ "1. No official education at all [No education]",
            EducationRUS == 2 ~ "2. Incomplete elementary [No education]",
            EducationRUS == 3 ~ "3. Complete elementary [Primary]",
            EducationRUS == 4 ~ "4. Incomplete secondary: a technical school, a vocational school [Primary]",
            EducationRUS == 5 ~ "5. Complete secondary: a technical school, a vocational school [Primary]",
            EducationRUS == 6 ~ "6. Incomplete higher [Primary]",
            EducationRUS == 7 ~ "7. Complete higher [College]",
            EducationRUS %in% c(98, 99) ~ NA_character_,
            # Spain
            EducationSPA == 1 ~ "1. No formal education [No education]",
            EducationSPA == 2 ~ "2. Primary studies incomplete [No education]",
            EducationSPA == 3 ~ "3. Primary studies complete (EGB 1) [Primary]",
            EducationSPA == 4 ~ "4. Secondary studies - stage 1 (EGB 2) [Primary]",
            EducationSPA == 5 ~ "5. Secondary studies - stage 2 [Primary]",
            EducationSPA == 6 ~ "6. Tertiary studies - college [Primary]",
            EducationSPA == 7 ~ "7. Tertiary studies - university [College]",
            EducationSPA %in% c(8, 9) ~ NA_character_,
            # Turkey
            EducationTUR == 1 ~ "1. No formal education [No education]",
            EducationTUR == 2 ~ "2. Incomplete primary school [No education]",
            EducationTUR == 3 ~ "3. Complete primary school [Primary]",
            EducationTUR == 4 ~ "4. Incomplete secondary school: technical/vocational type [Primary]",
            EducationTUR == 5 ~ "5. Complete secondary school: technical/vocational type [Primary]",
            EducationTUR == 6 ~ "6. Incomplete secondary: university-preparatory type [Primary]",
            EducationTUR == 7 ~ "7. Complete secondary: university-preparatory type [Primary]",
            EducationTUR == 8 ~ "8. Some university-level education, without a degree [Primary]",
            EducationTUR == 9 ~ "9. University-level education, with degree [College]",
            EducationTUR %in% c(98, 99) ~ NA_character_, 
            # USA
            EducationUSA == 1 ~ "1. None, or grade 1-8 [Primary]",
            EducationUSA == 2 ~ "2. High school incomplete (Grades 9-11) [Primary]",
            EducationUSA == 3 ~ "3. High school graduate (Grade 12 or GED certificate) [Primary]",
            EducationUSA == 4 ~ "4. Technical, trade or vocational school [Primary]",
            EducationUSA == 5 ~ "5. Some college, no 4-year degree [Primary]",
            EducationUSA == 6 ~ "6. College graduate [College]",
            EducationUSA == 7 ~ "7. Post-graduate training or professional schooling [College]",
            EducationUSA %in% c(8, 9) ~ NA_character_,
            # Japan
            EducationJAP == 1 ~ "1. No formal education [No education]",
            EducationJAP == 2 ~ "2. Incomplete junior high school [Primary]",
            EducationJAP == 3 ~ "3. Complete junior high school [Primary]",
            EducationJAP == 4 ~ "4. Incomplete high school [Primary]",
            EducationJAP == 5 ~ "5. Complete high school [Primary]",
            EducationJAP == 6 ~ "6. Currently in technical school [Primary]",
            EducationJAP == 7 ~ "7. Incomplete technical school [Primary]",
            EducationJAP == 8 ~ "8. Complete technical school [Primary]",
            EducationJAP == 9 ~ "9. Currently in junior college [Primary]",
            EducationJAP == 10 ~ "10. Incomplete junior college [Primary]",
            EducationJAP == 11 ~ "11. Complete junior college [Primary]",
            EducationJAP == 12 ~ "12. Currently in university [Primary]",
            EducationJAP == 13 ~ "13. Incomplete university [Primary]",
            EducationJAP == 14 ~ "14. Complete university [College]",
            EducationJAP == 15 ~ "15. Currently in post graduate [College]",
            EducationJAP == 16 ~ "16. Incomplete post graduate [College]",
            EducationJAP == 17 ~ "17. Complete post graduate [College]",
            EducationJAP %in% c(98, 99) ~ NA_character_
          ),
    
    # respondent's gender (numeric: female = 1; male = 0; other = NA)
      resp_female =
        case_when(
          Sex == 1 ~ 0,
          Sex == 2 ~ 1),
      
    # respondent resident in rural (vs urban) area (numeric: rural = 1; urban/semi-urban/peri-urban = 0)
      resp_rural = 
        case_when(
          usr == 2 ~ 1,
          usr == 1 ~ 0,
          
          usr1 == 3 ~ 1,
          usr1 %in% c(1,2) ~ 0,
          
          TRUE ~ NA_real_), # checked - no rural/urban variable; topline method detail says sampling in some countries was disproportionately/exclusively urban, but we only know India was exclusively urban 
    
    #########################  
    ### SOCIAL DISTANCE 1 ###  
    #########################
    
      # original question number; question text; response options (input above)
        resp_soc_dist_1_qinfo = "NUM: Q2g [Q2g.Fav.Jews]; QTEXT: Please tell me if you have a very favorable, somewhat favorable, somewhat unfavorable, or very unfavorable opinion of (INSERT)? Jews.; ROPTIONS: 1 = Very favorable [=0] + 2 = Somewhat favorable [=0] + 3 = Somewhat unfavorable [=1] + 4 = Very unfavorable [=1]; TARGET: Jewish; TYPE: Favorability",
      
      # original response (as character vector)
        resp_soc_dist_1_original = 
          dplyr::recode(
            as.character(Q2g_Fav_Jews),
            "8" = NA_character_,
            "9" = NA_character_),       

      # binary recode (numeric: 1 = any negative attitude expressed; 0 otherwise)
        resp_soc_dist_1_bin_recode = 
          case_when(
            Q2g_Fav_Jews %in% c(1:2) ~ 0,
            Q2g_Fav_Jews %in% c(3:4) ~ 1,
            TRUE ~ NA_real_),
    
    #########################  
    ### SOCIAL DISTANCE 2 ###  
    #########################
    
      # original question number; question text; response options (input above)
        resp_soc_dist_2_qinfo = "NUM: Q2h [Q2h.Fav.Christians]; QTEXT: Please tell me if you have a very favorable, somewhat favorable, somewhat unfavorable, or very unfavorable opinion of (INSERT)? Christians.; ROPTIONS: 1 = Very favorable [=0] + 2 = Somewhat favorable [=0] + 3 = Somewhat unfavorable [=1] + 4 = Very unfavorable [=1]; TARGET: Christian; TYPE: Favorability",
      
      # original response (as character vector)
        resp_soc_dist_2_original = 
          dplyr::recode(
            as.character(Q2h_Fav_Christians),
            "8" = NA_character_,
            "9" = NA_character_),       

      # binary recode (numeric: 1 = any negative attitude expressed; 0 otherwise)
        resp_soc_dist_2_bin_recode = 
          case_when(
            Q2h_Fav_Christians %in% c(1:2) ~ 0,
            Q2h_Fav_Christians %in% c(3:4) ~ 1,
            TRUE ~ NA_real_),
    
    #########################  
    ### SOCIAL DISTANCE 3 ###  
    #########################
    
      # original question number; question text; response options (input above)
        resp_soc_dist_3_qinfo = "NUM: Q2i [Q2i.Fav.Muslims]; QTEXT: Please tell me if you have a very favorable, somewhat favorable, somewhat unfavorable, or very unfavorable opinion of (INSERT)? Muslims.; ROPTIONS: 1 = Very favorable [=0] + 2 = Somewhat favorable [=0] + 3 = Somewhat unfavorable [=1] + 4 = Very unfavorable [=1]; TARGET: Muslim; TYPE: Favorability",
      
      # original response (as character vector)
        resp_soc_dist_3_original = 
          dplyr::recode(
            as.character(Q2i_Fav_Muslims),
            "8" = NA_character_,
            "9" = NA_character_),       

      # binary recode (numeric: 1 = any negative attitude expressed; 0 otherwise)
        resp_soc_dist_3_bin_recode = 
          case_when(
            Q2i_Fav_Muslims %in% c(1:2) ~ 0,
            Q2i_Fav_Muslims %in% c(3:4) ~ 1,
            TRUE ~ NA_real_),
    
    ############################  
    ### GENERAL SOCIAL TRUST ###  
    ############################
    
      # original question number; question text; response options (input above)
        resp_gentrust_qinfo = NA, # checked; no question asked

      # original response (as character vector)
        resp_gentrust_original = NA,       

      # binary recode (numeric: 1 = any negative attitude expressed; 0 otherwise)
        resp_gentrust_bin_recode = NA,    
    
    #########################  
    ##### RELIGIOSITY #######  
    #########################
    
      # original question number; question text; response options (input above)
        resp_religiosity_qinfo = "NUM: Religion_Import; QTEXT: How important is religion in your life – very important, somewhat important, not too important, or not at all important?; ROPTIONS: 1 = Very important + 2 = Somewhat important + 3 = Not too important + 4 = Not at all important", # note that this is not in the codebook 
  
      # original response (as numeric vector, with non-substantive responses coded as NA_real_)
        resp_religiosity_original = 
          dplyr::recode(
            as.numeric(importance_of_religion),
            `8` = NA_real_,
            `9` = NA_real_),       

      # recode (numeric: scaled 0-1, where 1 is more religious)
        resp_religiosity_recode = (4 - resp_religiosity_original)/3
    
    ) %>% 
    select(starts_with("resp_"))

```
Important note from the documentation on this survey round, which accounts for the oversampling of Muslims in UK, Germany, France, Spain:

"PEW RESEARCH CENTER - Global Attitudes Project
Spring 2006 15 Nation Survey


It is important to read this information before using the 2006 Pew Global Attitudes Survey dataset because there was some 
oversampling done of European Muslim populations for which a special weight and filter need to be used to get accurate results.

1.	This survey includes data for general populations and European Muslim populations. 
 
2.	There are specific weights that must be used for the general populations and the European Muslim populations.  A filter must also be used 
	when seeking to use the European Muslim oversamples.

	a.	The weight that must be used when looking to analyze the attitudes and opinions of general populations* is:
		
		General population variable name:  Weight
			

	*General population data from Great Britain, France, Germany, and Spain include Muslim oversamples, but is weighted to be representative of the 
	general population."
	
# Clean Pew Global Attitudes Spring 2008

```{r}
# declare dates
  dates_pew_gap_spring_08 <-
    tribble(
       ~resp_country_common, ~resp_interview_start_date, ~resp_interview_end_date,
       "Argentina", "2008-03-25", "2008-04-03",
       "Australia", "2008-03-20", "2008-04-04",
       "Brazil", "2008-03-20", "2008-04-08",
       "China", "2008-03-28", "2008-04-19",
       "Egypt", "2008-03-19", "2008-04-07",
       "France", "2008-03-31", "2008-04-08",
       "Germany", "2008-03-25", "2008-04-09",
       "UK", "2008-03-17", "2008-04-06",
       "India", "2008-04-01", "2008-04-16",
       "Indonesia", "2008-03-29", "2008-04-14",
       "Japan", "2008-03-19", "2008-04-13",
       "Jordan", "2008-03-18", "2008-04-06",
       "Lebanon", "2008-03-19", "2008-04-07",
       "Mexico", "2008-03-18", "2008-03-31",
       "Nigeria", "2008-04-08", "2008-04-21",
       "Pakistan", "2008-04-01", "2008-04-19",
       "Poland", "2008-03-26", "2008-04-14",
       "Russia", "2008-03-18", "2008-04-04",
       "South Africa", "2008-03-18", "2008-04-04",
       "South Korea", "2008-03-20", "2008-03-27",
       "Spain", "2008-03-17", "2008-04-17",
       "Tanzania", "2008-03-31", "2008-04-16",
       "Turkey", "2008-03-31", "2008-04-21",
       "US", "2008-04-09", "2008-04-17"
    ) %>%
    mutate(
        resp_interview_start_date = as.Date(resp_interview_start_date, "%Y-%m-%d"),
        resp_interview_end_date = as.Date(resp_interview_end_date, "%Y-%m-%d"))

# clean pew global attitudes spring 2008
  n4_clean_pew_gap_spring_08 <-
    pew_gap_spring_08_raw %>% 
    mutate(
      
    #########################  
    ####### META-DATA #######  
    #########################      
      
      # source name (character vector, title case)
        resp_source = "Pew Global Attitudes Project",
        
      # round number (character vector, title case)  
        resp_round = "Spring 2008",
      
      # url to dataset source, where publicly available (character vector)
        resp_original_data_url = "pewrsr.ch/3wToJeP",

      # survey mode (in-person/phone/internet/mail)
        resp_survey_mode = "in-person/phone",

      # country (character vector; list of countries as written in original source)
        resp_country_original = to_character(country),

      # survey mode (in-person/phone/internet/mail)
        resp_survey_mode = 
          case_when(
            resp_country_original %in% c(
              "Argentina",  
              "Brazil", 
              "China", 
              "Egypt", 
              "India", 
              "Indonesia", 
              "Jordan", 
              "Lebanon", 
              "Mexico", 
              "Nigeria", 
              "Pakistan", 
              "Poland", 
              "Russia", 
              "South Africa", 
              "South Korea", 
              "Tanzania", 
              "Turkey") ~ "in-person",
            
            resp_country_original %in% c(
              "Australia", 
              "Great Britain", 
              "France", 
              "Germany", 
              "Japan", 
              "Spain", 
              "United States") ~ "phone"),
    
      # country (character vector; converts to countrycode county.name list)
        resp_country_common = 
          countryname(resp_country_original),
        
      # interview date (variable of class Date; if only month given, input 1st of month)
        resp_interview_date = as.Date(as.character(Q103), "%d%m%Y")) %>%
    left_join(dates_pew_gap_spring_08, by = "resp_country_common") %>%
    mutate(
   
    #########################  
    ##### DEMOGRAPHICS ######  
    #########################
      
      # respondent denomination (character vector, recoded jointly below)
        across(starts_with("q38"), to_character)) %>% 
    mutate(
        resp_denomination = coalesce(!!! select(., starts_with("q38"))),

      # respondent's age (character vector; bins denoted by single dash ["18-25"])
        resp_age =
          dplyr::recode(
            as.character(Q76),
            "97" = "97+",
            "98" = NA_character_,
            "99" = NA_character_),        
      
      # respondent's education level
        resp_education_original =
          case_when(
            #Argentina
            Q84ARG == 1 ~ "1. No formal education [No education]",
            Q84ARG == 2 ~ "2. Incomplete primary school [No education]",
            Q84ARG == 3 ~ "3. Complete primary school [Primary]",
            Q84ARG == 4 ~ "4. Incomplete secondary school [Primary]",
            Q84ARG == 5 ~ "5. Complete secondary school [Primary]",
            Q84ARG == 6 ~ "6. Incomplete technical school [Primary]",
            Q84ARG == 7 ~ "7. Complete technical school [Primary]",
            Q84ARG == 8 ~ "8. Some university-level education, without a degree [Primary]",
            Q84ARG == 9 ~ "9. University-level education, with degree [College]",
            Q84ARG %in% c(98, 99) ~ NA_character_,
            #Australia
            Q84AUS == 1 ~ "1. Primary school [Primary]",
            Q84AUS == 2 ~ "2. Some Secondary/ Technical [Primary]",
            Q84AUS == 3 ~ "3. Intermediate/Form 4/Year 10  [Primary]",
            Q84AUS == 4 ~ "4. 5th form/Leaving/Year 11 [Primary]",
            Q84AUS == 5 ~ "5. Finished technical school/Matriculation /HSC/Year 12 [Primary]",
            Q84AUS == 6 ~ "6. Some/Now at University [Primary]",
            Q84AUS == 7 ~ "7. Degree from College of Advanced Education/ TAFE/Tertiary [Primary]",
            Q84AUS == 8 ~ "8. Degree from University/College of Advanced Education [College]",
            Q84AUS == 9 ~ "9. Higher Degree/Higher Diploma E.G. PH.D, Masters [College]",
            Q84AUS %in% c(98, 99) ~ NA_character_,
            #China
            Q84CHI == 1 ~ "1. Below primary school [No education]",
            Q84CHI == 2 ~ "2. Primary school [Primary]",
            Q84CHI == 3 ~ "3. Junior school [Primary]",
            Q84CHI == 4 ~ "4. High school [Primary]",
            Q84CHI == 5 ~ "5. College [Primary]", # assume this means degree not complete
            Q84CHI == 6 ~ "6. Bachelor's degree [College]",
            Q84CHI == 7 ~ "7. Master's degree [College]",
            Q84CHI == 8 ~ "8. Doctor's degree and above [College]",
            Q84CHI == 9 ~ "9. No formal education [No education]",
            Q84CHI == 97 ~ "97. No formal education [No education]", # odd but this is what the attributes are given as
            Q84CHI %in% c(98, 99) ~ NA_character_,
            #France
            Q84FRA == 1 ~ "1. No Formal Education [No education]",
            Q84FRA == 2 ~ "2. Primary school [Primary]",
            Q84FRA == 3 ~ "3. Secondary school: technical/vocational type [Primary]",
            Q84FRA == 4 ~ "4. Secondary school: university-preparatory type [Primary]",
            Q84FRA == 5 ~ "5. Some university-level education, with degree [Primary]",
            Q84FRA == 6 ~ "6. University-level education, with degree [College]",
            Q84FRA %in% c(8, 9) ~ NA_character_,
            #Germany
            Q84GER == 1 ~ "1. still attending school [Primary]",
            Q84GER == 2 ~ "2. lower secondary school without professional training [Primary]",
            Q84GER == 3 ~ "3. lower secondary school with professional training [Primary]",
            Q84GER == 4 ~ "4. middle secondary school without matriculation standard [Primary]",
            Q84GER == 5 ~ "5. general qualifications for university entrance [Primary]",
            Q84GER == 6 ~ "6. university degree [College]",
            Q84GER %in% c(8, 9) ~ NA_character_,
            #India
            Q84INDIA == 1 ~ "1. Illiterate [No education]",
            Q84INDIA == 2 ~ "2. Literate but no formal schooling  [No education]",
            Q84INDIA == 3 ~ "3. School up to 4 years [No education]",
            Q84INDIA == 4 ~ "4. School up to 5 to 9 years [Primary]",
            Q84INDIA == 5 ~ "5. SSC/HSC [Primary]",
            Q84INDIA == 6 ~ "6. Some college but not graduated [Primary]",
            Q84INDIA == 7 ~ "7. Graduate/Post grad-Gen BA MSc BCom etc [College]",
            Q84INDIA == 8 ~ "8. Graduate/Post grad-Prof BE MTech MBA MBBS etc [College]",
            Q84INDIA %in% c(98, 99) ~ NA_character_,
            #Japan
            Q84JAP == 1 ~ "1. No formal education [No education]",
            Q84JAP == 2 ~ "2. Incomplete junior high school [Primary]",
            Q84JAP == 3 ~ "3. Complete junior high school [Primary]",
            Q84JAP == 4 ~ "4. Incomplete high school [Primary]",
            Q84JAP == 5 ~ "5. Complete high school [Primary]",
            Q84JAP == 6 ~ "6. Currently in technical school [Primary]",
            Q84JAP == 7 ~ "7. Incomplete technical school [Primary]",
            Q84JAP == 8 ~ "8. Complete technical school [Primary]",
            Q84JAP == 9 ~ "9. Currently in junior college [Primary]",
            Q84JAP == 10 ~ "10. Incomplete junior college [Primary]",
            Q84JAP == 11 ~ "11. Complete junior college [Primary]",
            Q84JAP == 12 ~ "12. Currently in university [Primary]",
            Q84JAP == 13 ~ "13. Incomplete university [Primary]",
            Q84JAP == 14 ~ "14. Complete university [College]",
            Q84JAP == 15 ~ "15. Currently in post graduate [College]",
            Q84JAP == 16 ~ "16. Incomplete post graduate [College]",
            Q84JAP == 17 ~ "17. Complete post graduate [College]",
            Q84JAP %in% c(98, 99) ~ NA_character_,
            #Pakistan
            Q84PAK == 1 ~ "1. No formal education [No education]",
            Q84PAK == 2 ~ "2. No formal education but can read and write [No education]",
            Q84PAK == 3 ~ "3. Less than 5 classes [No education]", # less than 5 years indicated incomplete primary
            Q84PAK == 4 ~ "4. 5-9 classes [Primary]",
            Q84PAK == 5 ~ "5. Matric [Primary]",
            Q84PAK == 6 ~ "6. Intermediate [Primary]",
            Q84PAK == 7 ~ "7. Graduate [College]",
            Q84PAK == 8 ~ "8. Post-graduate [College]",
            Q84PAK %in% c(98, 99) ~ NA_character_,
            #Poland
            Q84POL == 1 ~ "1. No formal education [No education]",
            Q84POL == 2 ~ "2. Incomplete primary school [No education]",
            Q84POL == 3 ~ "3. Complete primary school [Primary]",
            Q84POL == 4 ~ "4. Vocational school (2-3 years) [Primary]",
            Q84POL == 5 ~ "5. Complete secondary school [Primary]",
            Q84POL == 6 ~ "6. Incomplete post secondary school [Primary]",
            Q84POL == 7 ~ "7. Complete post secondary school [Primary]",
            Q84POL == 8 ~ "8. Some university-level education, without a diploma [Primary]",
            Q84POL == 9 ~ "9. University-level education, with diploma [College]",
            Q84POL %in% c(98, 99) ~ NA_character_,
            #Russia
            Q84RUS == 1 ~ "1. No formal education [No education]",
            Q84RUS == 2 ~ "2. Incomplete primary school [No education]",
            Q84RUS == 3 ~ "3. Complete primary school [Primary]",
            Q84RUS == 4 ~ "4. Incomplete secondary: a technical school, a vocational school, general secondary school [Primary]",
            Q84RUS == 5 ~ "5. Complete secondary: a technical school, a vocational school, general secondary school [Primary]",
            Q84RUS == 6 ~ "6. Incomplete higher [Primary]",
            Q84RUS == 7 ~ "7. Complete higher [College]",
            Q84RUS %in% c(98, 99) ~ NA_character_,
            #South Africa
            Q84SAFR == 1 ~ "1. No formal education [No education]",
            Q84SAFR == 2 ~ "2. Incomplete primary school [No education]",
            Q84SAFR == 3 ~ "3. Complete primary school [Primary]",
            Q84SAFR == 4 ~ "4. Some high school/secondary school [Primary]",
            Q84SAFR == 5 ~ "5. Completed secondary/voc [Primary]",
            Q84SAFR == 6 ~ "6. College or Univ [College]",
            Q84SAFR == 7 ~ "7. Other [Primary]",
            #South Korea
            Q84SKOR == 1 ~ "1. No formal education [No education]",
            Q84SKOR == 2 ~ "2. Incomplete primary school [No education]",
            Q84SKOR == 3 ~ "3. Complete primary school [Primary]",
            Q84SKOR == 4 ~ "4. Incomplete secondary school: technical/vocational type [Primary]",
            Q84SKOR == 5 ~ "5. Complete secondary school: technical/vocational type [Primary]",
            Q84SKOR == 6 ~ "6. Incomplete secondary: university-preparatory type [Primary]",
            Q84SKOR == 7 ~ "7. Complete secondary: university-preparatory type [Primary]",
            Q84SKOR == 8 ~ "8. Some university-level education, without a degree [Primary]",
            Q84SKOR == 9 ~ "9. University-level education, with degree [College]",
            Q84SKOR %in% c(98, 99) ~ NA_character_,
            #Spain
            Q84SPA == 1 ~ "1. No formal education [No education]",
            Q84SPA == 2 ~ "2. Primary studies incomplete [No education]",
            Q84SPA == 3 ~ "3. Primary studies complete (EGB 1) [Primary]",
            Q84SPA == 4 ~ "4. Secondary studies - stage 1 (EGB 2) [Primary]",
            Q84SPA == 5 ~ "5. Secondary studies - stage 2 [Primary]",
            Q84SPA == 6 ~ "6. Tertiary studies - college [College]", # assume first stage
            Q84SPA == 7 ~ "7. Tertiary studies - university [College]",
            Q84SPA %in% c(8, 9) ~ NA_character_,
            #USA
            Q84USA == 1 ~ "1. None, or grade 1-8 [Primary]",
            Q84USA == 2 ~ "2. High school incomplete (Grades 9-11) [Primary]",
            Q84USA == 3 ~ "3. High school graduate (Grade 12 or GED certificate) [Primary]",
            Q84USA == 4 ~ "4. Technical, trade, or vocational school AFTER high school [Primary]",
            Q84USA == 5 ~ "5. Some college, no 4-year degree (including associate degree) [Primary]",
            Q84USA == 6 ~ "6. College graduate (B.S., B.A., or other 4-year degree) [College]",
            Q84USA == 7 ~ "7. Post-graduate training or professional schooling after college [College]",
            Q84USA %in% c(8, 9) ~ NA_character_,
            #Brazil
            q84BRA == 1 ~ "1. No formal education [No education]",
            q84BRA == 2 ~ "2. Incomplete primary school [No education]",
            q84BRA == 3 ~ "3. Complete primary school [Primary]",
            q84BRA == 4 ~ "4. Incomplete secondary school [Primary]",
            q84BRA == 5 ~ "5. Complete secondary school [Primary]",
            q84BRA == 6 ~ "6. Incomplete high school [Primary]",
            q84BRA == 7 ~ "7. Complete high school [Primary]",
            q84BRA == 8 ~ "8. Some university-level education, without a degree [Primary]",
            q84BRA == 9 ~ "9. University-level education, with degree [College]",
            q84BRA %in% c(10, 11) ~ NA_character_,
            #Egypt
            q84EGY == 1 ~ "1. No formal education [No education]",
            q84EGY == 2 ~ "2. Incomplete primary [No education]",
            q84EGY == 3 ~ "3. Complete primary [Primary]",
            q84EGY == 4 ~ "4. Incomplete secondary (vocational) [Primary]",
            q84EGY == 5 ~ "5. Complete secondary (vocational) [Primary]",
            q84EGY == 6 ~ "6. Incomplete secondary (preparatory) [Primary]",
            q84EGY == 7 ~ "7. Complete secondary (preparatory) [Primary]",
            q84EGY == 8 ~ "8. Some university without degree [Primary]",
            q84EGY == 9 ~ "9. University with degree [College]",
            q84EGY %in% c(98, 99) ~ NA_character_,
            #Britain
            q84GBR == 1 ~ "1. No qualifications obtained [Primary]", # this ia s hard call, but seems most consistent since primary is compulsory
            q84GBR == 2 ~ "2. GCSE or 'O' level grades D or below / CSE grades 2 or below [Primary]",
            q84GBR == 3 ~ "3. *GCSE or 'O' level grades A-C / CSE grade 1/ NVQ 2 [Primary]",
            q84GBR == 4 ~ "4. 'A' level / NVQ 3 [Primary]",
            q84GBR == 5 ~ "5. Degree / HND / teaching or nursing certificates / NVQ 4 [College]",
            q84GBR == 6 ~ "6. Higher degree (masters, PHD, NVQ 5) [College]",
            q84GBR == 7 ~ "7. Other qualification [Primary]",
            q84GBR %in% c(8, 9, 98, 99) ~ NA_character_,
            #Indonesia
            q84INDO == 1 ~ "1. No formal education [No education]", 
            q84INDO == 2 ~ "2. Incomplete primary school [No education]",
            q84INDO == 3 ~ "3. Complete primary school [Primary]",
            q84INDO == 4 ~ "4. Incomplete secondary school technical/vocational type [Primary]",
            q84INDO == 5 ~ "5. Complete secondary school technical/vocational type [Primary]",
            q84INDO == 6 ~ "6. Incomplete secondary University-preparatory type [Primary]",
            q84INDO == 7 ~ "7. Complete Secondary University-preparatory type [Primary]",
            q84INDO == 8 ~ "8. University-level education, without degree [Primary]",
            q84INDO == 9 ~ "9. University-level education, with degree [College]",
            q84INDO %in% c(98, 99) ~ NA_character_,
            #Jordan
            q84JOR == 1 ~ "1. No formal education [No education]",
            q84JOR == 2 ~ "2. Incomplete primary [No education]",
            q84JOR == 3 ~ "3. Complete primary [Primary]",
            q84JOR == 4 ~ "4. Incomplete secondary (vocational) [Primary]",
            q84JOR == 5 ~ "5. Complete secondary (vocational) [Primary]",
            q84JOR == 6 ~ "6. Incomplete secondary (preparatory) [Primary]",
            q84JOR == 7 ~ "7. Complete secondary (preparatory) [Primary]",
            q84JOR == 8 ~ "8. Some university without degree [Primary]",
            q84JOR == 9 ~ "9. University with degree [College]",
            q84JOR %in% c(98, 99) ~ NA_character_,
            #Lebanon
            q84LEB == 1 ~ "1. No formal education [No education]",
            q84LEB == 2 ~ "2. Incomplete primary [No education]",
            q84LEB == 3 ~ "3. Complete primary [Primary]",
            q84LEB == 4 ~ "4. Incomplete secondary (vocational) [Primary]",
            q84LEB == 5 ~ "5. Complete secondary (vocational) [Primary]",
            q84LEB == 6 ~ "6. Incomplete secondary (preparatory) [Primary]",
            q84LEB == 7 ~ "7. Complete secondary (preparatory) [Primary]",
            q84LEB == 8 ~ "8. Some university without degree [Primary]",
            q84LEB == 9 ~ "9. University with degree [College]",
            q84LEB %in% c(98, 99) ~ NA_character_,
            #Mexico
            q84MEX == 1 ~ "1. No formal education [No education]",
            q84MEX == 2 ~ "2. Incomplete primary school [No education]",
            q84MEX == 3 ~ "3. Complete primary school [Primary]",
            q84MEX == 4 ~ "4. Incomplete secondary school: technical/vocational type [Primary]",
            q84MEX == 5 ~ "5. Complete secondary school: technical/vocational type [Primary]",
            q84MEX == 6 ~ "6. Incomplete secondary: university-preparatory type [Primary]",
            q84MEX == 7 ~ "7. Complete secondary: university-preparatory type [Primary]",
            q84MEX == 8 ~ "8. Some university-level education, without a degree [Primary]",
            q84MEX == 9 ~ "9. University-level education, with degree [College]",
            q84MEX %in% c(10, 11) ~ NA_character_,
            #Nigeria
            q84NIG == 1 ~ "1. No formal education [No education]",
            q84NIG == 2 ~ "2. Incomplete primary school [No education]",
            q84NIG == 3 ~ "3. Complete primary school [Primary]",
            q84NIG == 4 ~ "4. Incomplete secondary school: technical/vocational type [Primary]",
            q84NIG == 5 ~ "5. Complete secondary school: technical/vocational type [Primary]",
            q84NIG == 6 ~ "6. Incomplete secondary: University-preparatory [Primary]",
            q84NIG == 7 ~ "7. Complete secondary: University-preparatroy [Primary]",
            q84NIG == 8 ~ "8. Incomplete College of Education [Primary]",
            q84NIG == 9 ~ "9. Complete College of Education [Primary]",
            q84NIG == 10 ~ "10. Incomplete Polytechnic [Primary]",
            q84NIG == 11 ~ "11. Complete Polytechnic [Primary]",
            q84NIG == 12 ~ "12. Some university-level education, without a degree [Primary]",
            q84NIG == 13 ~ "13. University-level education, with degree [College]",
            q84NIG %in% c(14, 15) ~ NA_character_,
            #Tanzania
            q84TAN == 1 ~ "1. No formal education [No education]",
            q84TAN == 2 ~ "2. Incomplete primary school [No education]",
            q84TAN == 3 ~ "3. Complete primary school [Primary]",
            q84TAN == 4 ~ "4. Incomplete secondary: Technical/vocational type [Primary]",
            q84TAN == 5 ~ "5. Complete secondary: Technical/vocational type [Primary]",
            q84TAN == 6 ~ "6. Incomplete secondary: University preparatory type [Primary]",
            q84TAN == 7 ~ "7. Complete secondary: University preparatory type [Primary]",
            q84TAN == 8 ~ "8. Some university-level education, without a degree [Primary]",
            q84TAN == 9 ~ "9. University- Level education, with a degree [College]",
            q84TAN %in% c(98, 99) ~ NA_character_,
            #Turkey
            q84TUR == 1 ~ "1. No formal education [No education]",
            q84TUR == 2 ~ "2. Incomplete primary school [No education]",
            q84TUR == 3 ~ "3. Complete primary school [Primary]",
            q84TUR == 4 ~ "4. Incomplete secondary school: technical/vocational type [Primary]",
            q84TUR == 5 ~ "5. Complete secondary school: technical/vocational type [Primary]",
            q84TUR == 6 ~ "6. Incomplete secondary: university-preparatory type [Primary]",
            q84TUR == 7 ~ "7. Complete secondary: university-preparatory type [Primary]",
            q84TUR == 8 ~ "8. Some university-level education, without a degree [Primary]",
            q84TUR == 9 ~ "9. University-level education, with degree [College]",
            q84TUR %in% c(98, 99) ~ NA_character_),       
      
      # respondent's gender (numeric: female = 1; male = 0; other = NA)
        resp_female =
          case_when(
            Q75 == 1 ~ 0,
            Q75 == 2 ~ 1),
      
      # respondent resident in rural (vs urban) area (numeric: rural = 1; urban/semi-urban/peri-urban = 0)
        across(starts_with("q101"), to_character)) %>% 
      mutate(
        merged_urban_rural = tolower(coalesce(!!! select(., starts_with("q101")))),
        resp_rural = 
          case_when(
            merged_urban_rural %in% c(
              "rural",
              "the countryside",
              "non metro",
               "rural/pedesaan"
            ) ~ 1,
            
            merged_urban_rural %in% c(
              "urban",
              "metropolitan city",
              "small/medium city",
              "a small town",
              "a city",
              "a large town",
              "suburban",
              "semi-suburban",
              "class 1 town",
              "metropolitan",
              "smaller town",
              "peripheral city, town or village",
              "urban/perkotaan",
              "metropolitan area",
              "city"
            ) ~ 0, # missing for the united states
            
            TRUE ~ NA_real_),
      
    #########################  
    ### SOCIAL DISTANCE 1 ###  
    #########################
    
      # original question number; question text; response options (input above)
        resp_soc_dist_1_qinfo = "NUM: Q10 [Q10e]; QTEXT: On a different topic, please tell me if you have a very favorable, somewhat favorable, somewhat unfavorable or very unfavorable opinion of Jews?; ROPTIONS: 1 = Very favorable [=0] + 2 = Somewhat favorable [=0] + 3 = Somewhat unfavorable [=1] + 4 = Very unfavorable [=1]; TARGET: Jewish; TYPE: Favorability",
      
      # original response (as character vector)
        resp_soc_dist_1_original = 
          dplyr::recode(
            as.character(Q10e),
            "8" = NA_character_,
            "9" = NA_character_),       

      # binary recode (numeric: 1 = any negative attitude expressed; 0 otherwise)
        resp_soc_dist_1_bin_recode = 
          case_when(
            Q10e %in% c(1:2) ~ 0,
            Q10e %in% c(3:4) ~ 1,
            TRUE ~ NA_real_),
    
    #########################  
    ### SOCIAL DISTANCE 2 ###  
    #########################
    
      # original question number; question text; response options (input above)
        resp_soc_dist_2_qinfo = "NUM: Q10 [Q10f]; QTEXT: On a different topic, please tell me if you have a very favorable, somewhat favorable, somewhat unfavorable or very unfavorable opinion of Christians?; ROPTIONS: 1 = Very favorable [=0] + 2 = Somewhat favorable [=0] + 3 = Somewhat unfavorable [=1] + 4 = Very unfavorable [=1]; TARGET: Christian; TYPE: Favorability",
      
      # original response (as character vector)
        resp_soc_dist_2_original = 
          dplyr::recode(
            as.character(Q10f),
            "8" = NA_character_,
            "9" = NA_character_),       

      # binary recode (numeric: 1 = any negative attitude expressed; 0 otherwise)
        resp_soc_dist_2_bin_recode = 
          case_when(
            Q10f %in% c(1:2) ~ 0,
            Q10f %in% c(3:4) ~ 1,
            TRUE ~ NA_real_),
    
    #########################  
    ### SOCIAL DISTANCE 3 ###  
    #########################
    
      # original question number; question text; response options (input above)
        resp_soc_dist_3_qinfo = "NUM: Q10 [Q10g]; QTEXT: On a different topic, please tell me if you have a very favorable, somewhat favorable, somewhat unfavorable or very unfavorable opinion of Muslims?; ROPTIONS: 1 = Very favorable [=0] + 2 = Somewhat favorable [=0] + 3 = Somewhat unfavorable [=1] + 4 = Very unfavorable [=1]; TARGET: Muslim; TYPE: Favorability",
      
      # original response (as character vector)
        resp_soc_dist_3_original = 
          dplyr::recode(
            as.character(Q10g),
            "8" = NA_character_,
            "9" = NA_character_),       

      # binary recode (numeric: 1 = any negative attitude expressed; 0 otherwise)
        resp_soc_dist_3_bin_recode = 
          case_when(
            Q10g %in% c(1:2) ~ 0,
            Q10g %in% c(3:4) ~ 1,
            TRUE ~ NA_real_),
    
    ############################  
    ### GENERAL SOCIAL TRUST ###  
    ############################
    
      # original question number; question text; response options (input above)
        resp_gentrust_qinfo = NA, # checked; no question asked

      # original response (as character vector)
        resp_gentrust_original = NA,       

      # binary recode (numeric: 1 = any negative attitude expressed; 0 otherwise)
        resp_gentrust_bin_recode = NA,    
    
    #########################  
    ##### RELIGIOSITY #######  
    #########################
    
      # original question number; question text; response options (input above)
        resp_religiosity_qinfo = "NUM: Q83; QTEXT: How important is religion in your life – very important, somewhat important, not too important, or not at all important?; ROPTIONS: 1 = Very important + 2 = Somewhat important + 3 = Not too important + 4 = Not at all important",
  
      # original response (as numeric vector, with non-substantive responses coded as NA_real_)
        resp_religiosity_original = 
          dplyr::recode(
            as.numeric(Q83),
            `8` = NA_real_,
            `9` = NA_real_),       

      # recode (numeric: scaled 0-1, where 1 is more religious)
        resp_religiosity_recode = (4 - resp_religiosity_original)/3
    
    ) %>% 
    select(starts_with("resp_"))
```

# Clean Pew Global Attitudes Spring 2009

```{r}
# declare dates
  dates_pew_gap_spring_09 <-
    tribble(
       ~resp_country_common, ~resp_interview_start_date, ~resp_interview_end_date,
       "Argentina", "2009-05-18", "2009-06-03",
       "Brazil", "2009-05-18", "2009-06-14",
       "Canada", "2009-05-19", "2009-06-05",
       "China", "2009-05-22", "2009-06-10",
       "Egypt", "2009-05-24", "2009-06-11",
       "France", "2009-05-29", "2009-06-09",
       "Germany", "2009-05-25", "2009-06-06",
       "UK", "2009-05-26", "2009-06-09",
       "India", "2009-05-22", "2009-06-13",
       "Indonesia", "2009-05-29", "2009-06-05",
       "Israel", "2009-05-18", "2009-06-16",
       "Japan", "2009-05-20", "2009-06-10",
       "Jordan", "2009-05-24", "2009-06-11",
       "Kenya", "2009-05-22", "2009-05-30",
       "Lebanon", "2009-05-20", "2009-06-03",
       "Mexico", "2009-05-26", "2009-06-02",
       "Nigeria", "2009-06-02", "2009-06-14",
       "Pakistan", "2009-05-22", "2009-06-09",
       "Palestine", "2009-05-23", "2009-06-11",
       "Poland", "2009-05-21", "2009-06-08",
       "Russia", "2009-05-20", "2009-06-09",
       "South Korea", "2009-05-25", "2009-06-08",
       "Spain", "2009-05-25", "2009-06-09",
       "Turkey", "2009-05-20", "2009-06-15",
       "US", "2009-05-27", "2009-06-10"
    ) %>%
    mutate(
        resp_interview_start_date = as.Date(resp_interview_start_date, "%Y-%m-%d"),
        resp_interview_end_date = as.Date(resp_interview_end_date, "%Y-%m-%d"))

# clean pew global attitudes spring 2009
  n5_clean_pew_gap_spring_09 <-
    pew_gap_spring_09_raw %>% 
    mutate(
      
    #########################  
    ####### META-DATA #######  
    #########################      
      
      # source name (character vector, title case)
        resp_source = "Pew Global Attitudes Project",
        
      # round number (character vector, title case)  
        resp_round = "Spring 2009",
      
      # url to dataset source, where publicly available (character vector)
        resp_original_data_url = "pewrsr.ch/3wToJeP",

      # country (character vector; list of countries as written in original source)
        resp_country_original = to_character(COUNTRY),
    
      # survey mode (in-person/phone/internet/mail)
        resp_survey_mode = 
          case_when(
            resp_country_original %in% c(
              "Argentina", 
              "Brazil", 
              "China", 
              "Egypt", 
              "India", 
              "Indonesia", 
              "Israel", 
              "Jordan", 
              "Kenya", 
              "Lebanon", 
              "Mexico", 
              "Nigeria", 
              "Pakistan", 
              "Palestinian Territories", 
              "Poland", 
              "Russia", 
              "South Korea", 
              "Turkey") ~ "in-person",
            
            resp_country_original %in% c(
              "Great Britain", 
              "Canada", 
              "France", 
              "Germany", 
              "Japan", 
              "Spain", 
              "United States") ~ "phone"),

      # country (character vector; converts to countrycode county.name list)
        resp_country_common = 
          countryname(resp_country_original),
        
      # interview date (variable of class Date; if only month given, input 1st of month)
        resp_interview_date = as.Date(as.character(Q112), "%d%m%Y")) %>%
    left_join(dates_pew_gap_spring_09, by = "resp_country_common") %>%
    mutate(
   
    #########################  
    ##### DEMOGRAPHICS ######  
    #########################
      
      # respondent denomination (character vector from master list)
        q41usa = RELIG, # us has a different variable name; var not available for china
        across(starts_with("q41"), to_character)) %>% 
        mutate(
        resp_denomination = coalesce(!!! select(., starts_with("q41"))),

      # respondent's age (character vector; bins denoted by single dash ["18-25"])
        resp_age =
          dplyr::recode(
            as.character(Q81),
            "97" = "97+",
            "98" = NA_character_,
            "99" = NA_character_
          ),        
      
      # respondent's education level
        resp_education_original =
          case_when(
            #Argentina
            Q95ARG == 1 ~ "1. No formal education [No education]",
            Q95ARG == 2 ~ "2. Incomplete primary school [No education]",
            Q95ARG == 3 ~ "3. Complete primary school [Primary]",
            Q95ARG == 4 ~ "4. Incomplete secondary school [Primary]",
            Q95ARG == 5 ~ "5. Complete secondary school [Primary]",
            Q95ARG == 6 ~ "6. Incomplete tertiary school [Primary]",
            Q95ARG == 7 ~ "7. Complete tertiary school [Primary]",
            Q95ARG == 8 ~ "8. Incomplete university [Primary]",
            Q95ARG == 9 ~ "9. Complete university [College]",
            Q95ARG %in% c(98, 99) ~ NA_character_,
            #Brazil
            Q95BRA == 1 ~ "1. No formal education [No education]",
            Q95BRA == 2 ~ "2. Incomplete primary [No education]",
            Q95BRA == 3 ~ "3. Complete primary [Primary]",
            Q95BRA == 4 ~ "4. Incomplete middle school [Primary]",
            Q95BRA == 5 ~ "5. Complete middle school [Primary]",
            Q95BRA == 6 ~ "6. Incomplete high school [Primary]",
            Q95BRA == 7 ~ "7. Complete high school [Primary]",
            Q95BRA == 8 ~ "8. Incomplete university [Primary]",
            Q95BRA == 9 ~ "9. Complete university [College]",
            Q95BRA %in% c(98, 99) ~ NA_character_,
            #Britain
            Q95BRI == 1 ~ "1. No qualifications obtained [Primary]",
            Q95BRI == 2 ~ "2. GCSE or 'O' level grades D or below/CSE grades 2 or below [Primary]",
            Q95BRI == 3 ~ "3. GCSE or 'O' level grades A-C/CSE grade 1/NVQ 2 [Primary]",
            Q95BRI == 4 ~ "4. 'A' level / NVQ 3 [Primary]",
            Q95BRI == 5 ~ "5. Degree/HND/teaching or nursing certificates/NVQ 4 [College]",
            Q95BRI == 6 ~ "6. Higher degree (masters, PHD, NVQ 5) [College]",
            Q95BRI == 7 ~ "7. Other qualification [Primary]",
            Q95BRI %in% c(98, 99) ~ NA_character_,
            #Canada
            Q95CAN == 1 ~ "1. Some elementary [No education]",
            Q95CAN == 2 ~ "2. Completed elementary [Primary]",
            Q95CAN == 3 ~ "3. Some high school [Primary]",
            Q95CAN == 4 ~ "4. Completed high school [Primary]",
            Q95CAN == 5 ~ "5. Community college/vocational/trade school/commercial/CEGEP [Primary]",
            Q95CAN == 6 ~ "6. Some university [Primary]",
            Q95CAN == 7 ~ "7. Completed university [College]",
            Q95CAN == 8 ~ "8. Post-graduate university/professional school [College]",
            Q95CAN %in% c(98, 99) ~ NA_character_,
            #China
            Q95CHI == 1 ~ "1. Below primary school [No education]",
            Q95CHI == 2 ~ "2. Primary school [Primary]",
            Q95CHI == 3 ~ "3. Junior school [Primary]",
            Q95CHI == 4 ~ "4. High school or technical secondary school [Primary]",
            Q95CHI == 5 ~ "5. College [Primary]",
            Q95CHI == 6 ~ "6. Bachelor's degree [College]",
            Q95CHI == 7 ~ "7. Master's degree [College]",
            Q95CHI == 8 ~ "8. Doctoral degree and above [College]",
            Q95CHI == 97 ~ "9. No formal education [No education]",
            Q95CHI %in% c(98, 99) ~ NA_character_,
            #Egypt
            Q95EGY == 1 ~ "1. No formal education [No education]",
            Q95EGY == 2 ~ "2. Illiterate [No education]",
            Q95EGY == 3 ~ "3. Complete elementary [Primary]",
            Q95EGY == 4 ~ "4. Incomplete intermediate [Primary]",
            Q95EGY == 5 ~ "5. Incomplete secondary [Primary]",
            Q95EGY == 6 ~ "6. Complete high school [Primary]",
            Q95EGY == 7 ~ "7. Complete College [Primary]", # implies below university
            Q95EGY == 8 ~ "8. Completed university in Middle East/Africa [College]",
            Q95EGY == 9 ~ "9. Completed university in Western Europe/ America [College]",
            Q95EGY == 10 ~ "10. Completed Masters/Post Graduate/ PhD [College]",
            Q95EGY %in% c(98, 99) ~ NA_character_,
            #France
            Q95FRA == 1 ~ "1. No formal education [No education]",
            Q95FRA == 2 ~ "2. Primary school [Primary]",
            Q95FRA == 3 ~ "3. Secondary school: technical/vocational type [Primary]", 
            Q95FRA == 4 ~ "4. Secondary school: university-preparatory type [Primary]",
            Q95FRA == 5 ~ "5. Post-secondary school, with degree [Primary]",
            Q95FRA == 6 ~ "6. University-level education, with degree [College]",
            Q95FRA %in% c(8, 9) ~ NA_character_,
            #Germany
            Q95GER == 1 ~ "1. Still attending school [Primary]",
            Q95GER == 2 ~ "2. Lower secondary school without professional training [Primary]",
            Q95GER == 3 ~ "3. Lower secondary school with professional training [Primary]",
            Q95GER == 4 ~ "4. Middle secondary school without matriculation standard [Primary]",
            Q95GER == 5 ~ "5. General qualifications for university entrance [Primary]",
            Q95GER == 6 ~ "6. University degree [College]",
            Q95GER %in% c(8, 9) ~ NA_character_,
            #Indonesia
            Q95INDO == 1 ~ "1. No formal education [No education]",
            Q95INDO == 2 ~ "2. Incomplete primary school [No education]",
            Q95INDO == 3 ~ "3. Complete primary school [Primary]",
            Q95INDO == 4 ~ "4. Incomplete secondary school technical/vocational type [Primary]",
            Q95INDO == 5 ~ "5. Complete secondary school technical/vocational type [Primary]",
            Q95INDO == 6 ~ "6. Incomplete secondary University-preparatory type [Primary]",
            Q95INDO == 7 ~ "7. Complete secondary University-preparatory type [Primary]",
            Q95INDO == 8 ~ "8. University-level education,without degree [Primary]",
            Q95INDO == 9 ~ "9. University-level education,with degree [College]",
            Q95INDO %in% c(98, 99) ~ NA_character_,
            #India
            Q95INDIA == 1 ~ "1. Illiterate [No education]",
            Q95INDIA == 2 ~ "2. Literate but no formal schooling [No education]",
            Q95INDIA == 3 ~ "3. School up to 4 years [No education]", # not 5 years
            Q95INDIA == 4 ~ "4. School up to 5 to 9 years [Primary]",
            Q95INDIA == 5 ~ "5. SSC/HSC [Primary]",
            Q95INDIA == 6 ~ "6. Some college but not graduated [Primary]",
            Q95INDIA == 7 ~ "7. Graduate/Post grad-Gen BA MSc BCom etc [College]",
            Q95INDIA == 8 ~ "8. Graduate/Post grad-Prof BE MTech MBA MBBS etc [College]",
            Q95INDIA %in% c(98, 99) ~ NA_character_,
            #Israel
            Q95ISR == 1 ~ "1. No formal education [No education]",
            Q95ISR == 2 ~ "2. Partial elementary [No education]",
            Q95ISR == 3 ~ "3. Completed elementary [Primary]",
            Q95ISR == 4 ~ "4. Partial junior high school [Primary]",
            Q95ISR == 5 ~ "5. Completed junior high school [Primary]",
            Q95ISR == 6 ~ "6. Partial high school [Primary]",
            Q95ISR == 7 ~ "7. Completed high school [Primary]",
            Q95ISR == 8 ~ "8. Partial tertiary education [Primary]",
            Q95ISR == 9 ~ "9. Completed tertiary education [College]", # hard call; but seems to suggest BA
            Q95ISR == 10 ~ "10. Academic education [College]",
            Q95ISR %in% c(98, 99) ~ NA_character_,
            #Japan
            Q95JAP == 1 ~ "1. No formal education [No education]",
            Q95JAP == 2 ~ "2. Incomplete junior high school [Primary]",
            Q95JAP == 3 ~ "3. Complete junior high school [Primary]",
            Q95JAP == 4 ~ "4. Incomplete high school [Primary]",
            Q95JAP == 5 ~ "5. Complete high school [Primary]",
            Q95JAP == 6 ~ "6. Currently in technical school [Primary]",
            Q95JAP == 7 ~ "7. Incomplete technical school [Primary]",
            Q95JAP == 8 ~ "8. Complete technical school [Primary]",
            Q95JAP == 9 ~ "9. Currently in junior College [Primary]",
            Q95JAP == 10 ~ "10. Incomplete junior College [Primary]",
            Q95JAP == 11 ~ "11. Complete junior College [Primary]",
            Q95JAP == 12 ~ "12. Currently in university [Primary]",
            Q95JAP == 13 ~ "13. Incomplete university [Primary]",
            Q95JAP == 14 ~ "14. Complete university [College]",
            Q95JAP == 15 ~ "15. Currently in post graduate [College]",
            Q95JAP == 16 ~ "16. Incomplete post graduate [College]",
            Q95JAP == 17 ~ "17. Complete post graduate [College]",
            Q95JAP %in% c(98, 99) ~ NA_character_,
            #Jordan
            Q95JOR == 1 ~ "1. No formal education [No education]",
            Q95JOR == 2 ~ "2. Incomplete primary [No education]",
            Q95JOR == 3 ~ "3. Complete primary [Primary]",
            Q95JOR == 4 ~ "4. Incomplete secondary (vocational) [Primary]",
            Q95JOR == 5 ~ "5. Complete secondary (vocational) [Primary]",
            Q95JOR == 6 ~ "6. Incomplete secondary (preparatory) [Primary]",
            Q95JOR == 7 ~ "7. Complete secondary (preparatory) [Primary]",
            Q95JOR == 8 ~ "8. Some university without degree [Primary]",
            Q95JOR == 9 ~ "9. University with degree [College]",
            Q95JOR %in% c(98, 99) ~ NA_character_,
            #Kenya
            Q95KEN == 1 ~ "1. No formal education [No education]",
            Q95KEN == 2 ~ "2. Incomplete primary school [No education]",
            Q95KEN == 3 ~ "3. Complete primary school [Primary]",
            Q95KEN == 4 ~ "4. Incomplete secondary school [Primary]",
            Q95KEN == 5 ~ "5. Complete secondary school [Primary]",
            Q95KEN == 6 ~ "6. Some college (Diploma/Certificate) [Primary]",
            Q95KEN == 7 ~ "7. Completed college (Diploma/Certificate) [Primary]",
            Q95KEN == 8 ~ "8. Some university level education [Primary]",
            Q95KEN == 9 ~ "9. University-level education, with degree [College]",
            Q95KEN %in% c(98, 99) ~ NA_character_,
            #Lebanon
            Q95LEB == 1 ~ "1. No formal education [No education]",
            Q95LEB == 2 ~ "2. Incomplete primary [No education]",
            Q95LEB == 3 ~ "3. Complete primary [Primary]",
            Q95LEB == 4 ~ "4. Incomplete secondary (vocational) [Primary]",
            Q95LEB == 5 ~ "5. Complete secondary (vocational) [Primary]",
            Q95LEB == 6 ~ "6. nComplete secondary (preparatory) [Primary]",
            Q95LEB == 7 ~ "7. Complete secondary (preparatory) [Primary]",
            Q95LEB == 8 ~ "8. Some university without degree [Primary]",
            Q95LEB == 9 ~ "9. University with degree [College]",
            Q95LEB %in% c(98, 99) ~ NA_character_,
            #Mexico
            Q95MEX == 1 ~ "1. No formal education [No education]",
            Q95MEX == 2 ~ "2. Incomplete primary school [No education]",
            Q95MEX == 3 ~ "3. Complete primary school [Primary]",
            Q95MEX == 4 ~ "4. Incomplete secondary school: technical/vocational type [Primary]",
            Q95MEX == 5 ~ "5. Complete secondary school: technical/vocational type [Primary]",
            Q95MEX == 6 ~ "6. Incomplete secondary: university-preparatory type [Primary]",
            Q95MEX == 7 ~ "7. Complete secondary: university-preparatory type [Primary]",
            Q95MEX == 8 ~ "8. Some university-level education, without a degree [Primary]",
            Q95MEX == 9 ~ "9. University-level education, with degree [College]",
            Q95MEX %in% c(10, 11) ~ NA_character_,
            #Nigeria
            Q95NIG == 1 ~ "1. No formal education [No education]",
            Q95NIG == 2 ~ "2. Incomplete primary school [No education]",
            Q95NIG == 3 ~ "3. Complete primary school [Primary]",
            Q95NIG == 4 ~ "4. Incomplete secondary school: technical/vocational type [Primary]",
            Q95NIG == 5 ~ "5. Complete secondary school: technical/vocational type [Primary]",
            Q95NIG == 6 ~ "6. Incomplete secondary: University-preparatory [Primary]",
            Q95NIG == 7 ~ "7. Complete secondary: University-preparatory [Primary]",
            Q95NIG == 8 ~ "8. Incomplete College of Education [Primary]",
            Q95NIG == 9 ~ "9. Complete College of Education [Primary]",
            Q95NIG == 10 ~ "10. Incomplete Polytechnic [Primary]",
            Q95NIG == 11 ~ "11. Complete Polytechnic [Primary]",
            Q95NIG == 12 ~ "12. Some university-level education, without a degree [Primary]",
            Q95NIG == 13 ~ "13. University-level education, with degree [College]",
            Q95NIG %in% c(14, 15) ~ NA_character_,
            #Pakistan
            Q95PAK == 1 ~ "1. No formal education [No education]",
            Q95PAK == 2 ~ "2. No formal education, but knows how to read and write [No education]",
            Q95PAK == 3 ~ "3. Less than primary [No education]",
            Q95PAK == 4 ~ "4. School 5-9 Years [Primary]",
            Q95PAK == 5 ~ "5. Matriculation [Primary]",
            Q95PAK == 6 ~ "6. Intermediate [Primary]",
            Q95PAK == 7 ~ "7. Graduate [College]",
            Q95PAK == 8 ~ "8. Post Graduate [College]",
            Q95PAK %in% c(98, 99) ~ NA_character_,
            #Palestine
            Q95PAL == 1 ~ "1. No formal education [No education]",
            Q95PAL == 2 ~ "2. Have not finished elementary school [No education]",
            Q95PAL == 3 ~ "3. Have finished elementary school [Primary]",
            Q95PAL == 4 ~ "4. Have not finished vocational high school [Primary]",
            Q95PAL == 5 ~ "5. Have finished vocational high school [Primary]",
            Q95PAL == 6 ~ "6. Have not finished high school- preparation for university studies [Primary]",
            Q95PAL == 7 ~ "7. Have finished high school-preparation for university studies [Primary]",
            Q95PAL == 8 ~ "8. Enrolled university but have not obtained a degree [Primary]",
            Q95PAL == 9 ~ "9. Enrolled a university and have obtained a degree [College]",
            Q95PAL %in% c(98, 99) ~ NA_character_,
            #Poland
            Q95POL == 1 ~ "1. No formal education [No education]",
            Q95POL == 2 ~ "2. Incomplete primary school [No education]",
            Q95POL == 3 ~ "3. Complete primary school [Primary]",
            Q95POL == 4 ~ "4. Vocational school (2-3 years) [Primary]",
            Q95POL == 5 ~ "5. Complete secondary school [Primary]",
            Q95POL == 6 ~ "6. Incomplete post secondary school [Primary]",
            Q95POL == 7 ~ "7. Complete post secondary school [Primary]",
            Q95POL == 8 ~ "8. Some university-level education, without a diploma [Primary]",
            Q95POL == 9 ~ "9. University-level education, with diploma [College]",
            Q95POL %in% c(98, 99) ~ NA_character_,
            #Russia
            Q95RUS == 1 ~ "1. No formal education [No education]",
            Q95RUS == 2 ~ "2. Primary education (up to 4 classes, inclusive) [Primary]",
            Q95RUS == 3 ~ "3. Secondary Education (up to 8-9 classes) [Primary]",
            Q95RUS == 4 ~ "4. Secondary school (high school or vocational school) [Primary]",
            Q95RUS == 5 ~ "5. Secondary education: College [Primary]",
            Q95RUS == 6 ~ "6. Incomplete higher education (at least 3 courses) [Primary]",
            Q95RUS == 7 ~ "7. Completed higher education [College]",
            Q95RUS %in% c(98, 99) ~ NA_character_,
            #South Korea
            Q95SKOR == 1 ~ "1. No formal education [No education]",
            Q95SKOR == 2 ~ "2. Incomplete elementary school [No education]",
            Q95SKOR == 3 ~ "3. Complete elementary school [Primary]",
            Q95SKOR == 4 ~ "4. Incomplete Junior High school [Primary]",
            Q95SKOR == 5 ~ "5. Complete Junior High school [Primary]",
            Q95SKOR == 6 ~ "6. Incomplete high school [Primary]",
            Q95SKOR == 7 ~ "7. Complete high school [Primary]",
            Q95SKOR == 8 ~ "8. Incomplete College/University or Some College [Primary]",
            Q95SKOR == 9 ~ "9. Complete College/University [College]",
            Q95SKOR %in% c(98, 99) ~ NA_character_,
            #Spain
            Q95SPA == 1 ~ "1. No formal education [No education]",
            Q95SPA == 2 ~ "2. Primary studies incomplete [Primary]",
            Q95SPA == 3 ~ "3. Primary studies complete (EGB 1) [Primary]",
            Q95SPA == 4 ~ "4. Secondary studies - stage 1 (EGB 2) [Primary]",
            Q95SPA == 5 ~ "5. Secondary studies - stage 2 [Primary]",
            Q95SPA == 6 ~ "6. Tertiary studies - College [College]", #assume first stage
            Q95SPA == 7 ~ "7. Tertiary studies - university [College]",
            Q95SPA %in% c(8, 9) ~ NA_character_,
            #Turkey
            Q95TUR == 1 ~ "1. No formal education [No education]",
            Q95TUR == 2 ~ "2. Incomplete primary school [No education]",
            Q95TUR == 3 ~ "3. Complete primary school [Primary]",
            Q95TUR == 4 ~ "4. Incomplete secondary school: technical/vocational type [Primary]",
            Q95TUR == 5 ~ "5. Complete secondary school: technical/vocational type [Primary]",
            Q95TUR == 6 ~ "6. Incomplete secondary: university-preparatory type [Primary]",
            Q95TUR == 7 ~ "7. Complete secondary: university-preparatory type [Primary]",
            Q95TUR == 8 ~ "8. Some university-level education, without a degree [Primary]",
            Q95TUR == 9 ~ "9. University-level education, with degree [College]",
            Q95TUR %in% c(98, 99) ~ NA_character_,
            #US
            Q95US == 1 ~ "1. None, or grade 1-8 [Primary]",
            Q95US == 2 ~ "2. High school incomplete (Grades 9-11) [Primary]",
            Q95US == 3 ~ "3. High school graduate (Grade 12 or GED certificate) [Primary]",
            Q95US == 4 ~ "4. Technical, trade, or vocational school AFTER high school [Primary]",
            Q95US == 5 ~ "5. Some college, no 4-year degree (including associate degree) [Primary]",
            Q95US == 6 ~ "6. College graduate (B.S., B.A., or other 4-year degree) [College]",
            Q95US == 7 ~ "7. Post-graduate training or professional schooling after college (e.g., toward a master's Degree or Ph.D., law or medical [College]",
            Q95US %in% c(8, 9) ~ NA_character_),       
      
      # respondent's gender (numeric: female = 1; male = 0; other = NA)
        resp_female =
          case_when(
            Q80 == 1 ~ 0,
            Q80 == 2 ~ 1),
      
      # respondent resident in rural (vs urban) area (numeric: rural = 1; urban/semi-urban/peri-urban = 0)
        resp_rural =
          case_when(
            Q110BRI %in% c(1:3)|
            Q110CHI %in% c(1:2)|
            Q110FRA %in% c(2:5)|
            Q110GER %in% c(1:2)|
            Q110JAP == 1|
            Q110KEN %in% c(1:3)|
            Q110MEX %in% c(1, 3)|
            Q110SKOR %in% c(1:2)|
            Q110SPA %in% c(1:2)|
            Q110UR == 1|
            Q110USR %in% c(1:2) ~ 0,
            
            Q110BRI == 4|
            Q110CHI == 3|
            Q110FRA == 1|
            Q110GER == 3|
            Q110JAP == 2|
            Q110KEN == 4|
            Q110MEX == 2|
            Q110SKOR == 3|
            Q110SPA == 3|
            Q110UR == 2|
            Q110USR == 3 ~ 1,
            
            TRUE ~ NA_real_),
      
    #########################  
    ### SOCIAL DISTANCE 1 ###  
    #########################
    
      # original question number; question text; response options (input above)
        resp_soc_dist_1_qinfo = "NUM: Q11 [Q11G]; QTEXT: Please tell me if you have a very favorable, somewhat favorable, somewhat unfavorable or very unfavorable opinion of Jews?; ROPTIONS: 1 = Very favorable [=0] + 2 = Somewhat favorable [=0] + 3 = Somewhat unfavorable [=1] + 4 = Very unfavorable [=1]; TARGET: Jewish; TYPE: Favorability",
      
      # original response (as character vector)
        resp_soc_dist_1_original = 
          dplyr::recode(
            as.character(Q11G),
            "8" = NA_character_,
            "9" = NA_character_),       

      # binary recode (numeric: 1 = any negative attitude expressed; 0 otherwise)
        resp_soc_dist_1_bin_recode = 
          case_when(
            Q11G %in% c(1:2) ~ 0,
            Q11G %in% c(3:4) ~ 1,
            TRUE ~ NA_real_),
    
    #########################  
    ### SOCIAL DISTANCE 2 ###  
    #########################
    
      # original question number; question text; response options (input above)
        resp_soc_dist_2_qinfo = "NUM: Q11 [Q11H]; QTEXT: Please tell me if you have a very favorable, somewhat favorable, somewhat unfavorable or very unfavorable opinion of Christians?; ROPTIONS: 1 = Very favorable [=0] + 2 = Somewhat favorable [=0] + 3 = Somewhat unfavorable [=1] + 4 = Very unfavorable [=1]; TARGET: Christian; TYPE: Favorability",
      
      # original response (as character vector)
        resp_soc_dist_2_original = 
          dplyr::recode(
            as.character(Q11H),
            "8" = NA_character_,
            "9" = NA_character_),       

      # binary recode (numeric: 1 = any negative attitude expressed; 0 otherwise)
        resp_soc_dist_2_bin_recode = 
          case_when(
            Q11H %in% c(1:2) ~ 0,
            Q11H %in% c(3:4) ~ 1,
            TRUE ~ NA_real_),
    
    #########################  
    ### SOCIAL DISTANCE 3 ###  
    #########################
    
      # original question number; question text; response options (input above)
        resp_soc_dist_3_qinfo = "NUM: Q11 [Q11I]; QTEXT: Please tell me if you have a very favorable, somewhat favorable, somewhat unfavorable or very unfavorable opinion of Muslims?; ROPTIONS: 1 = Very favorable [=0] + 2 = Somewhat favorable [=0] + 3 = Somewhat unfavorable [=1] + 4 = Very unfavorable [=1]; TARGET: Muslim; TYPE: Favorability",
      
      # original response (as character vector)
        resp_soc_dist_3_original = 
          dplyr::recode(
            as.character(Q11I),
            "8" = NA_character_,
            "9" = NA_character_),       

      # binary recode (numeric: 1 = any negative attitude expressed; 0 otherwise)
        resp_soc_dist_3_bin_recode = 
          case_when(
            Q11I %in% c(1:2) ~ 0,
            Q11I %in% c(3:4) ~ 1,
            TRUE ~ NA_real_),
    
    ############################  
    ### GENERAL SOCIAL TRUST ###  
    ############################
    
      # original question number; question text; response options (input above)
        resp_gentrust_qinfo = NA, # checked; no question asked

      # original response (as character vector)
        resp_gentrust_original = NA,       

      # binary recode (numeric: 1 = any negative attitude expressed; 0 otherwise)
        resp_gentrust_bin_recode = NA,    
    
    #########################  
    ##### RELIGIOSITY #######  
    #########################
    
      # original question number; question text; response options (input above)
        resp_religiosity_qinfo = "NUM: Q93; QTEXT: How important is religion in your life – very important, somewhat important, not too important, or not at all important?; ROPTIONS: 1 = Very important + 2 = Somewhat important + 3 = Not too important + 4 = Not at all important",
  
      # original response (as numeric vector, with non-substantive responses coded as NA_real_)
        resp_religiosity_original = 
          dplyr::recode(
            as.numeric(Q93),
            `8` = NA_real_,
            `9` = NA_real_),       

      # recode (numeric: scaled 0-1, where 1 is more religious)
        resp_religiosity_recode = (4 - resp_religiosity_original)/3
    
    ) %>% 
    select(starts_with("resp_"))
```

# Clean Pew Global Attitudes Fall 2009

```{r}
# declare dates
  dates_pew_gap_fall_09 <-
    tribble(
      ~resp_country_common, ~resp_interview_start_date, ~resp_interview_end_date,
      "UK", "Sep. 1, 2009", "Sep. 11, 2009",
      "Bulgaria", "Aug. 27, 2009", "Sep. 19, 2009",
      "Czechia", "Aug. 30, 2009", "Sep. 14, 2009",
      "France", "Sep. 8, 2009", "Sep. 15, 2009",
      "Germany", "Aug. 31, 2009", "Sep. 19, 2009",
      "Hungary", "Aug. 31, 2009", "Sep. 13, 2009",
      "Italy", "Sep. 10, 2009", "Sep. 22, 2009",
      "Lithuania", "Sep. 3, 2009", "Sep. 19, 2009",
      "Poland", "Sep. 1, 2009", "Sep. 15, 2009",
      "Russia", "Aug. 28, 2009", "Sep. 24, 2009",
      "Slovakia", "Sep. 2, 2009", "Sep. 17, 2009",
      "Spain", "Aug. 31, 2009", "Sep. 16, 2009",
      "Ukraine", "Sep. 2, 2009", "Sep. 10, 2009",
      "US", "Sep. 10, 2009", "Sep. 15, 2009"
    ) %>%
    mutate(
      resp_interview_start_date = as.Date(resp_interview_start_date, "%b. %d, %Y"),
      resp_interview_end_date = as.Date(resp_interview_end_date, "%b. %d, %Y"))

# clean pew global attitudes fall 2009
  n6_clean_pew_gap_fall_09 <-
    pew_gap_fall_09_raw %>% 
    mutate(
      
    #########################  
    ####### META-DATA #######  
    #########################      
      
      # source name (character vector, title case)
        resp_source = "Pew Global Attitudes Project",
        
      # round number (character vector, title case)  
        resp_round = "Fall 2009",
      
      # url to dataset source, where publicly available (character vector)
        resp_original_data_url = "pewrsr.ch/3wToJeP",

      # country (character vector; list of countries as written in original source)
        resp_country_original = to_character(COUNTRY),
    
      # survey mode (in-person/phone/internet/mail)
        resp_survey_mode = 
          case_when(
            resp_country_original %in% c(
              "Bulgaria", 
              "Czech Republic", 
              "Hungary", 
              "Italy", 
              "Lithuania", 
              "Poland", 
              "Russia", 
              "Slovakia", 
              "Ukraine") ~ "in-person",
            
            resp_country_original %in% c(
              "Britain", 
              "France", 
              "Germany", 
              "Spain", 
              "US") ~ "phone"),
      
      # country (character vector; converts to countrycode county.name list)
        resp_country_common = 
          countryname(resp_country_original),
        
      # interview date (variable of class Date; if only month given, input 1st of month)
        resp_interview_date = as.Date(as.character(Q79), "%d%m%Y")) %>%
    left_join(dates_pew_gap_fall_09, by = "resp_country_common") %>%
    mutate(
   
    #########################  
    ##### DEMOGRAPHICS ######  
    #########################
      
      # respondent denomination (character vector from master list)
      q67usa = RELIG,  
      across(starts_with("q67"), to_character)) %>% 
    mutate(
        resp_denomination = coalesce(!!! select(., starts_with("q67"))),
        resp_denomination = 
          case_when(
            resp_denomination %in% c(
              "Catholic", # necessary because one of the target sects is "Roman Catholic", below (Greek Catholic not included)
              "Roman Catholic", 
              "Roman Catholic (Catholic)", 
              "Yes, I am of the Roman-Catholic faith"
              ) ~ "Roman Catholic",
            TRUE ~ resp_denomination),

      # respondent's age (character vector; bins denoted by single dash ["18-25"])
        resp_age =
          case_when(
            COUNTRY == 5 ~ as.character(Q17),
            COUNTRY != 5 ~ as.character(Q54),
            Q17 == 97 | Q54 == 97 ~ "97+",
            Q17 %in% c(98:99) | Q17 %in% c(98:99) ~ NA_character_,
            TRUE ~ NA_character_),
      
      # respondent's education level
        resp_education_original =
          case_when(
            #Britain
            Q60BRI == 1 ~ "1. No qualifications obtained [Primary]", # given compulsory primary
            Q60BRI == 2 ~ "2. GCSE or 'O' level grades D or below / CSE grades 2 or below [Primary]",
            Q60BRI == 3 ~ "3. *GCSE or 'O' level grades A-C / CSE grade 1/ NVQ 2 [Primary]",
            Q60BRI == 4 ~ "4. 'A' level / NVQ 3 [Primary]",
            Q60BRI == 5 ~ "5. Degree / HND / teaching or nursing certificates / NVQ 4 [College]",
            Q60BRI == 6 ~ "6. Higher degree (masters, PHD, NVQ 5) [College]",
            Q60BRI == 7 ~ "7. Other qualification [Primary]",
            Q60BRI %in% c(8, 9) ~ NA_character_,
            #Bulgaria
            Q60BUL == 1 ~ "1. Without education [No education]",
            Q60BUL == 2 ~ "2. Uncompleted basic education [No education]",
            Q60BUL == 3 ~ "3. Completed basic education [Primary]",
            Q60BUL == 4 ~ "4. Uncompleted secondary education - technical/professional [Primary]",
            Q60BUL == 5 ~ "5. Completed secondary education - technical/professional [Primary]",
            Q60BUL == 6 ~ "6. Uncompleted common education - high school [Primary]",
            Q60BUL == 7 ~ "7. Completed common education - high school [Primary]",
            Q60BUL == 8 ~ "8. Some kind of high education degree, without diploma [Primary]",
            Q60BUL == 9 ~ "9. Completed high education with diploma [College]",
            Q60BUL %in% c(98, 99) ~ NA_character_,
            #Czech Republic
            Q60CZE == 1 ~ "1. No education [No education]",
            Q60CZE == 2 ~ "2. Uncompleted Primary school [No education]",
            Q60CZE == 3 ~ "3. Completed Primary school [Primary]",
            Q60CZE == 4 ~ "4. High school without diploma [Primary]",
            Q60CZE == 5 ~ "5. High school with diploma [Primary]",
            Q60CZE == 6 ~ "6. Uncompleted bachelor or advanced vocational training [Primary]",
            Q60CZE == 7 ~ "7. Completed bachelor or advanced vocational training [Primary]", # because less than university 
            Q60CZE == 8 ~ "8. Uncompleted university [Primary]", 
            Q60CZE == 9 ~ "9. Completed university [College]",
            Q60CZE %in% c(98, 99) ~ NA_character_,
            #France
            Q60FRA == 1 ~ "1. No formal education [No education]",
            Q60FRA == 2 ~ "2. Primary school [Primary]",
            Q60FRA == 3 ~ "3. Secondary school: technical/vocational type [Primary]",
            Q60FRA == 4 ~ "4. Secondary school: university-preparatory type [Primary]",
            Q60FRA == 5 ~ "5. Post-secondary school, with degree [Primary]",
            Q60FRA == 6 ~ "6. University-level education, with degree [College]",
            Q60FRA %in% c(8, 9) ~ NA_character_,
            #Germany
            Q60GER == 1 ~ "1. Still attending school [Primary]",
            Q60GER == 2 ~ "2. Lower secondary school without professional training [Primary]",
            Q60GER == 3 ~ "3. Lower secondary school with professional training [Primary]",
            Q60GER == 4 ~ "4. Middle secondary school without matriculation standard [Primary]",
            Q60GER == 5 ~ "5. General qualifications for university entrance [Primary]",
            Q60GER == 6 ~ "6. University degree [College]",
            Q60GER %in% c(8, 9) ~ NA_character_,
            #Hungary
            Q60HUN == 1 ~ "1. Less than 8 grades in Primary school [No education]", # Implies incomplete primary school, according to their own system
            Q60HUN == 2 ~ "2. 8 grades [Primary]",
            Q60HUN == 3 ~ "3. Secondary school [Primary]",
            Q60HUN == 4 ~ "4. University [College]",
            Q60HUN %in% c(8, 9) ~ NA_character_,
            #Italy
            Q60ITA == 1 ~ "9. University degree [College]",
            Q60ITA == 2 ~ "8. University without degree [Primary]",
            Q60ITA == 3 ~ "7. Senior high school with diploma [Primary]",
            Q60ITA == 4 ~ "6. Senior high school without diploma [Primary]",
            Q60ITA == 5 ~ "5. Junior high school with diploma [Primary]",
            Q60ITA == 6 ~ "4. Junior high school without diploma [Primary]",
            Q60ITA == 7 ~ "3. Elementary school with leaving certificate [Primary]",
            Q60ITA == 8 ~ "2. Elementary school without leaving certificate [No education]",
            Q60ITA == 9 ~ "1. No schooling [No education]",
            #Lithuania
            Q60LIT == 0 ~ "0. Not completed primary [No education]", 
            Q60LIT == 1 ~ "1. Primary [Primary]",
            Q60LIT == 2 ~ "2. Basic [Primary]",
            Q60LIT == 3 ~ "3. Secondary [Primary]",
            Q60LIT == 4 ~ "4. Vocational school [Primary]",
            Q60LIT == 5 ~ "5. Higher non-university (vocational college) [Primary]",
            Q60LIT == 6 ~ "6. Higher university (university, academy) [College]",
            Q60LIT == 7 ~ "7. Doctoral studies [College]",
            Q60LIT %in% c(98, 99) ~ NA_character_,
            #Poland
            Q60POL == 1 ~ "1. No formal education [No education]",
            Q60POL == 2 ~ "2. Incomplete primary school [No education]",
            Q60POL == 3 ~ "3. Complete primary school [Primary]",
            Q60POL == 4 ~ "4. Vocational school (2-3 years) [Primary]",
            Q60POL == 5 ~ "5. Complete secondary school [Primary]",
            Q60POL == 6 ~ "6. Incomplete post secondary school [Primary]",
            Q60POL == 7 ~ "7. Complete post secondary school [Primary]",
            Q60POL == 8 ~ "8. Some university-level education, without a diploma [Primary]",
            Q60POL == 9 ~ "9. University-level education, with diploma [College]",
            #Russia
            Q60RUS == 1 ~ "1. No formal education [No education]",
            Q60RUS == 2 ~ "2. Primary education (up to 4 classes, inclusive) [Primary]",
            Q60RUS == 3 ~ "3. Secondary Education (up to 8-9 classes) [Primary]",
            Q60RUS == 4 ~ "4. Secondary school (high school or vocational school) [Primary]",
            Q60RUS == 5 ~ "5. Secondary education: College [Primary]",
            Q60RUS == 6 ~ "6. Incomplete higher education (at least 3 courses) [Primary]",
            Q60RUS == 7 ~ "7. Completed higher education [College]",
            Q60RUS %in% c(98, 99) ~ NA_character_,
            #Slovakia
            Q60SLO == 1 ~ "1. No education [No education]",
            Q60SLO == 2 ~ "2. Uncompleted Primary school [No education]",
            Q60SLO == 3 ~ "3. Completed Primary school [Primary]",
            Q60SLO == 4 ~ "4. High school without diploma [Primary]",
            Q60SLO == 5 ~ "5. High school with diploma [Primary]",
            Q60SLO == 6 ~ "6. Uncompleted bachelor or advanced vocational training [Primary]",
            Q60SLO == 7 ~ "7. Completed bachelor or advanced vocational training [Primary]", # consistent with CR above
            Q60SLO == 8 ~ "8. Uncompleted university [Primary]",
            Q60SLO == 9 ~ "9. Completed university [College]",
            Q60SLO %in% c(98, 99) ~ NA_character_,
            #Spain
            Q60SPA == 1 ~ "1. Without studies [No education]",
            Q60SPA == 2 ~ "2. Not completed primary education [No education]",
            Q60SPA == 3 ~ "3. Primary or first stage of basic (EGB1) [Primary]",
            Q60SPA == 4 ~ "4. Secondary Studies - 1st. Cycle (EGB2) [Primary]",
            Q60SPA == 5 ~ "5. Secondary Studies - 2nd. Cycle [Primary]",
            Q60SPA == 6 ~ "6. First stage of tertiary/ The Third degree [College]",
            Q60SPA == 7 ~ "7. Second stage of tertiary [College]",
            Q60SPA %in% c(8, 9) ~ NA_character_,
            #Ukraine
            Q60UKR == 1 ~ "1. No formal education [No education]",
            Q60UKR == 2 ~ "2. Incomplete primary education [No education]",
            Q60UKR == 3 ~ "3. Primary education [Primary]",
            Q60UKR == 4 ~ "4. Incomplete secondary education [Primary]",
            Q60UKR == 5 ~ "5. Secondary education [Primary]",
            Q60UKR == 6 ~ "6. Vocational education [Primary]",
            Q60UKR == 7 ~ "7. Incomplete vocational education [Primary]",
            Q60UKR == 8 ~ "8. Incomplete higher education [Primary]",
            Q60UKR == 9 ~ "9. Higher education [College]", # assumed this means BA given previous option
            Q60UKR == 10 ~ "10. Degree (Candidate of science \\ Doctor) [College]",
            Q60UKR %in% c(98, 99) ~ NA_character_,
            #US
            Q60US == 1 ~ "1. None, or grade 1-8 [Primary]", #BOD given 5 years
            Q60US == 2 ~ "2. High school incomplete (Grades 9-11) [Primary]",
            Q60US == 3 ~ "3. High school graduate (Grade 12 or GED certificate) [Primary]",
            Q60US == 4 ~ "4. Technical, trade, or vocational school AFTER high school [Primary]",
            Q60US == 5 ~ "5. Some college, no 4-year degree (including associate degree) [Primary]",
            Q60US == 6 ~ "6. College graduate (B.S., B.A., or other 4-year degree) [College]",
            Q60US == 7 ~ "7. Post-graduate training or professional schooling after College [College]",
            Q60US %in% c(8, 9) ~ NA_character_),       
      
      # respondent's gender (numeric: female = 1; male = 0; other = NA)
        resp_female =
          case_when(
            Q53 == 1 ~ 0,
            Q53 == 2 ~ 1),
      
      # respondent resident in rural (vs urban) area (numeric: rural = 1; urban/semi-urban/peri-urban = 0)
        resp_rural =
          case_when(
            Q77BRI %in% c(1:3)| 
            Q77BUL %in% c(1:3)|
            Q77FRA %in% c(2:5)|
            Q77GER %in% c(1:2)|
            Q77HUN %in% c(1:3)|
            Q77SPA %in% c(1:2)|
            Q77USR %in% c(1:2)|
            Q77UR == 1 ~ 0,
            
            Q77BRI == 4|
            Q77BUL == 4|
            Q77FRA == 1|
            Q77GER == 3|
            Q77HUN == 4|
            Q77SPA == 3|
            Q77USR == 3|
            Q77UR == 0 ~ 1,
            
            TRUE ~ NA_real_),
      
    #########################  
    ### SOCIAL DISTANCE 1 ###  
    #########################
    
      # original question number; question text; response options (input above)
        resp_soc_dist_1_qinfo = "NUM: Q22 [Q22A]; QTEXT: I'd like you to rate some different groups of people in (survey country) according to how you feel about them. For each group, please tell me whether your opinion is very favorable, mostly favorable, mostly unfavorable or very unfavorable? Jews.; ROPTIONS: 1 = Very favorable [=0] + 2 = Mostly favorable [=0] + 3 = Mostly unfavorable [=1] + 4 = Very unfavorable [=1]; TARGET: Jewish; TYPE: Favorability",
      
      # original response (as character vector)
        resp_soc_dist_1_original = 
          dplyr::recode(
            as.character(Q22A),
            "8" = NA_character_,
            "9" = NA_character_),       

      # binary recode (numeric: 1 = any negative attitude expressed; 0 otherwise)
        resp_soc_dist_1_bin_recode = 
          case_when(
            Q22A %in% c(1:2) ~ 0,
            Q22A %in% c(3:4) ~ 1,
            TRUE ~ NA_real_),
    
    #########################  
    ### SOCIAL DISTANCE 2 ###  
    #########################
    
      # original question number; question text; response options (input above)
        resp_soc_dist_2_qinfo = "NUM: Q22 [Q22B]; QTEXT: I'd like you to rate some different groups of people in (survey country) according to how you feel about them. For each group, please tell me whether your opinion is very favorable, mostly favorable, mostly unfavorable or very unfavorable? Catholics.; ROPTIONS: 1 = Very favorable [=0] + 2 = Mostly favorable [=0] + 3 = Mostly unfavorable [=1] + 4 = Very unfavorable [=1]; TARGET: Roman Catholic; TYPE: Favorability",
      
      # original response (as character vector)
        resp_soc_dist_2_original = 
          dplyr::recode(
            as.character(Q22B),
            "8" = NA_character_,
            "9" = NA_character_),       

      # binary recode (numeric: 1 = any negative attitude expressed; 0 otherwise)
        resp_soc_dist_2_bin_recode = 
          case_when(
            Q22B %in% c(1:2) ~ 0,
            Q22B %in% c(3:4) ~ 1,
            TRUE ~ NA_real_),
    
    #########################  
    ### SOCIAL DISTANCE 3 ###  
    #########################
    
      # original question number; question text; response options (input above)
        resp_soc_dist_3_qinfo = "NUM: Q22 [Q22C]; QTEXT: I'd like you to rate some different groups of people in (survey country) according to how you feel about them. For each group, please tell me whether your opinion is very favorable, mostly favorable, mostly unfavorable or very unfavorable? Muslims.; ROPTIONS: 1 = Very favorable [=0] + 2 = Mostly favorable [=0] + 3 = Mostly unfavorable [=1] + 4 = Very unfavorable [=1]; TARGET: Muslim; TYPE: Favorability",
      
      # original response (as character vector)
        resp_soc_dist_3_original = 
          dplyr::recode(
            as.character(Q22C),
            "8" = NA_character_,
            "9" = NA_character_),       

      # binary recode (numeric: 1 = any negative attitude expressed; 0 otherwise)
        resp_soc_dist_3_bin_recode = 
          case_when(
            Q22C %in% c(1:2) ~ 0,
            Q22C %in% c(3:4) ~ 1,
            TRUE ~ NA_real_),
    
    #########################  
    ### SOCIAL DISTANCE 4 ###  
    #########################
    
      # original question number; question text; response options (input above)
        resp_soc_dist_4_qinfo = "NUM: Q22 [Q22D]; QTEXT: I'd like you to rate some different groups of people in (survey country) according to how you feel about them. For each group, please tell me whether your opinion is very favorable, mostly favorable, mostly unfavorable or very unfavorable? Hindus.; ROPTIONS: 1 = Very favorable [=0] + 2 = Mostly favorable [=0] + 3 = Mostly unfavorable [=1] + 4 = Very unfavorable [=1]; TARGET: Hindu; TYPE: Favorability",
      
      # original response (as character vector)
        resp_soc_dist_4_original = 
          dplyr::recode(
            as.character(Q22D),
            "8" = NA_character_,
            "9" = NA_character_),       

      # binary recode (numeric: 1 = any negative attitude expressed; 0 otherwise)
        resp_soc_dist_4_bin_recode = 
          case_when(
            Q22D %in% c(1:2) ~ 0,
            Q22D %in% c(3:4) ~ 1,
            TRUE ~ NA_real_),
    
    #########################
    ### SOCIAL DISTANCE 5 ###  
    #########################
    
      # original question number; question text; response options (input above)
        resp_soc_dist_5_qinfo = "NUM: Q23b [Q23BA]; QTEXT: I'd like you to rate some different groups of people in (survey country) according to how you feel about them. For each group, please tell me whether your opinion is very favorable, mostly favorable, mostly unfavorable or very unfavorable? Jews.; ROPTIONS: 1 = Very favorable [=0] + 2 = Mostly favorable [=0] + 3 = Mostly unfavorable [=1] + 4 = Very unfavorable [=1]; TARGET: Jewish; TYPE: Favorability",
      
      # original response (as character vector)
        resp_soc_dist_5_original = 
          dplyr::recode(
            as.character(Q23BA),
            "8" = NA_character_,
            "9" = NA_character_),       

      # binary recode (numeric: 1 = any negative attitude expressed; 0 otherwise)
        resp_soc_dist_5_bin_recode = 
          case_when(
            Q23BA %in% c(1:2) ~ 0,
            Q23BA %in% c(3:4) ~ 1,
            TRUE ~ NA_real_),
    
    #########################
    ### SOCIAL DISTANCE 6 ###  
    #########################
    
      # original question number; question text; response options (input above)
        resp_soc_dist_6_qinfo = "NUM: Q23b [Q23BB]; QTEXT: I'd like you to rate some different groups of people in (survey country) according to how you feel about them. For each group, please tell me whether your opinion is very favorable, mostly favorable, mostly unfavorable or very unfavorable? Christians.; ROPTIONS: 1 = Very favorable [=0] + 2 = Mostly favorable [=0] + 3 = Mostly unfavorable [=1] + 4 = Very unfavorable [=1]; TARGET: Christian; TYPE: Favorability",
      
      # original response (as character vector)
        resp_soc_dist_6_original = 
          dplyr::recode(
            as.character(Q23BB),
            "8" = NA_character_,
            "9" = NA_character_),       

      # binary recode (numeric: 1 = any negative attitude expressed; 0 otherwise)
        resp_soc_dist_6_bin_recode = 
          case_when(
            Q23BB %in% c(1:2) ~ 0,
            Q23BB %in% c(3:4) ~ 1,
            TRUE ~ NA_real_),
    
    #########################
    ### SOCIAL DISTANCE 7 ###  
    #########################
    
      # original question number; question text; response options (input above)
        resp_soc_dist_7_qinfo = "NUM: Q23b [Q23BC]; QTEXT: I'd like you to rate some different groups of people in (survey country) according to how you feel about them. For each group, please tell me whether your opinion is very favorable, mostly favorable, mostly unfavorable or very unfavorable? Muslims.; ROPTIONS: 1 = Very favorable [=0] + 2 = Mostly favorable [=0] + 3 = Mostly unfavorable [=1] + 4 = Very unfavorable [=1]; TARGET: Muslim; TYPE: Favorability",
      
      # original response (as character vector)
        resp_soc_dist_7_original = 
          dplyr::recode(
            as.character(Q23BC),
            "8" = NA_character_,
            "9" = NA_character_),       

      # binary recode (numeric: 1 = any negative attitude expressed; 0 otherwise)
        resp_soc_dist_7_bin_recode = 
          case_when(
            Q23BC %in% c(1:2) ~ 0,
            Q23BC %in% c(3:4) ~ 1,
            TRUE ~ NA_real_),
    
    #########################
    ### SOCIAL DISTANCE 8 ###  
    #########################
    
      # original question number; question text; response options (input above)
        resp_soc_dist_8_qinfo = "NUM: Q24 [Q24B]; QTEXT: I'd like you to rate some different groups of people in (survey country) according to how you feel about them. For each group, please tell me whether your opinion is very favorable, mostly favorable, mostly unfavorable or very unfavorable? Jews.; ROPTIONS: 1 = Very favorable [=0] + 2 = Mostly favorable [=0] + 3 = Mostly unfavorable [=1] + 4 = Very unfavorable [=1]; TARGET: Jewish; TYPE: Favorability",
      
      # original response (as character vector)
        resp_soc_dist_8_original = 
          dplyr::recode(
            as.character(Q24B),
            "8" = NA_character_,
            "9" = NA_character_),       

      # binary recode (numeric: 1 = any negative attitude expressed; 0 otherwise)
        resp_soc_dist_8_bin_recode = 
          case_when(
            Q24B %in% c(1:2) ~ 0,
            Q24B %in% c(3:4) ~ 1,
            TRUE ~ NA_real_),
    
    #########################
    ### SOCIAL DISTANCE 9 ###  
    #########################
    
      # original question number; question text; response options (input above)
        resp_soc_dist_9_qinfo = "NUM: Q25 [Q25A]; QTEXT: I'd like you to rate some different groups of people in (survey country) according to how you feel about them. For each group, please tell me whether your opinion is very favorable, mostly favorable, mostly unfavorable or very unfavorable? Muslims.; ROPTIONS: 1 = Very favorable [=0] + 2 = Mostly favorable [=0] + 3 = Mostly unfavorable [=1] + 4 = Very unfavorable [=1]; TARGET: Muslim; TYPE: Favorability",
      
      # original response (as character vector)
        resp_soc_dist_9_original = 
          dplyr::recode(
            as.character(Q25A),
            "8" = NA_character_,
            "9" = NA_character_),       

      # binary recode (numeric: 1 = any negative attitude expressed; 0 otherwise)
        resp_soc_dist_9_bin_recode = 
          case_when(
            Q25A %in% c(1:2) ~ 0,
            Q25A %in% c(3:4) ~ 1,
            TRUE ~ NA_real_),
    
    #########################
    ### SOCIAL DISTANCE 10 ##  
    #########################
    
      # original question number; question text; response options (input above)
        resp_soc_dist_10_qinfo = "NUM: Q26 [Q26A]; QTEXT: I'd like you to rate some different groups of people in (survey country) according to how you feel about them. For each group, please tell me whether your opinion is very favorable, mostly favorable, mostly unfavorable or very unfavorable? Jews.; ROPTIONS: 1 = Very favorable [=0] + 2 = Mostly favorable [=0] + 3 = Mostly unfavorable [=1] + 4 = Very unfavorable [=1]; TARGET: Jewish; TYPE: Favorability",
      
      # original response (as character vector)
        resp_soc_dist_10_original = 
          dplyr::recode(
            as.character(Q26A),
            "8" = NA_character_,
            "9" = NA_character_),       

      # binary recode (numeric: 1 = any negative attitude expressed; 0 otherwise)
        resp_soc_dist_10_bin_recode = 
          case_when(
            Q26A %in% c(1:2) ~ 0,
            Q26A %in% c(3:4) ~ 1,
            TRUE ~ NA_real_),
    
    #########################
    ### SOCIAL DISTANCE 11 ##
    #########################
    
      # original question number; question text; response options (input above)
        resp_soc_dist_11_qinfo = "NUM: Q27 [Q27D]; QTEXT: I'd like you to rate some different groups of people in (survey country) according to how you feel about them. For each group, please tell me whether your opinion is very favorable, mostly favorable, mostly unfavorable or very unfavorable? Jews.; ROPTIONS: 1 = Very favorable [=0] + 2 = Mostly favorable [=0] + 3 = Mostly unfavorable [=1] + 4 = Very unfavorable [=1]; TARGET: Jewish; TYPE: Favorability",
      
      # original response (as character vector)
        resp_soc_dist_11_original = 
          dplyr::recode(
            as.character(Q27D),
            "8" = NA_character_,
            "9" = NA_character_),       

      # binary recode (numeric: 1 = any negative attitude expressed; 0 otherwise)
        resp_soc_dist_11_bin_recode = 
          case_when(
            Q27D %in% c(1:2) ~ 0,
            Q27D %in% c(3:4) ~ 1,
            TRUE ~ NA_real_),
    
    #########################
    ### SOCIAL DISTANCE 12 ##
    #########################
    
      # original question number; question text; response options (input above)
        resp_soc_dist_12_qinfo = "NUM: Q28 [Q28C]; QTEXT: I'd like you to rate some different groups of people in (survey country) according to how you feel about them. For each group, please tell me whether your opinion is very favorable, mostly favorable, mostly unfavorable or very unfavorable? Jews.; ROPTIONS: 1 = Very favorable [=0] + 2 = Mostly favorable [=0] + 3 = Mostly unfavorable [=1] + 4 = Very unfavorable [=1]; TARGET: Jewish; TYPE: Favorability",
      
      # original response (as character vector)
        resp_soc_dist_12_original = 
          dplyr::recode(
            as.character(Q28C),
            "8" = NA_character_,
            "9" = NA_character_),       

      # binary recode (numeric: 1 = any negative attitude expressed; 0 otherwise)
        resp_soc_dist_12_bin_recode = 
          case_when(
            Q28C %in% c(1:2) ~ 0,
            Q28C %in% c(3:4) ~ 1,
            TRUE ~ NA_real_),
    
    #########################
    ### SOCIAL DISTANCE 13 ##
    #########################
    
      # original question number; question text; response options (input above)
        resp_soc_dist_13_qinfo = "NUM: Q29 [Q29C]; QTEXT: I'd like you to rate some different groups of people in (survey country) according to how you feel about them. For each group, please tell me whether your opinion is very favorable, mostly favorable, mostly unfavorable or very unfavorable? Jews.; ROPTIONS: 1 = Very favorable [=0] + 2 = Mostly favorable [=0] + 3 = Mostly unfavorable [=1] + 4 = Very unfavorable [=1]; TARGET: Jewish; TYPE: Favorability",
      
      # original response (as character vector)
        resp_soc_dist_13_original = 
          dplyr::recode(
            as.character(Q29C),
            "8" = NA_character_,
            "9" = NA_character_),       

      # binary recode (numeric: 1 = any negative attitude expressed; 0 otherwise)
        resp_soc_dist_13_bin_recode = 
          case_when(
            Q29C %in% c(1:2) ~ 0,
            Q29C %in% c(3:4) ~ 1,
            TRUE ~ NA_real_),
    
    #########################
    ### SOCIAL DISTANCE 14 ##
    #########################
    
      # original question number; question text; response options (input above)
        resp_soc_dist_14_qinfo = "NUM: Q30 [Q30B]; QTEXT: I'd like you to rate some different groups of people in (survey country) according to how you feel about them. For each group, please tell me whether your opinion is very favorable, mostly favorable, mostly unfavorable or very unfavorable? Muslim Bulgarians.; ROPTIONS: 1 = Very favorable [=0] + 2 = Mostly favorable [=0] + 3 = Mostly unfavorable [=1] + 4 = Very unfavorable [=1]; TARGET: Muslim; TYPE: Favorability",
      
      # original response (as character vector)
        resp_soc_dist_14_original = 
          dplyr::recode(
            as.character(Q30B),
            "8" = NA_character_,
            "9" = NA_character_),       

      # binary recode (numeric: 1 = any negative attitude expressed; 0 otherwise)
        resp_soc_dist_14_bin_recode = 
          case_when(
            Q30B %in% c(1:2) ~ 0,
            Q30B %in% c(3:4) ~ 1,
            TRUE ~ NA_real_),
    
    #########################
    ### SOCIAL DISTANCE 15 ##
    #########################
    
      # original question number; question text; response options (input above)
        resp_soc_dist_15_qinfo = "NUM: Q30 [Q30C]; QTEXT: I'd like you to rate some different groups of people in (survey country) according to how you feel about them. For each group, please tell me whether your opinion is very favorable, mostly favorable, mostly unfavorable or very unfavorable? Jews.; ROPTIONS: 1 = Very favorable [=0] + 2 = Mostly favorable [=0] + 3 = Mostly unfavorable [=1] + 4 = Very unfavorable [=1]; TARGET: Jewish; TYPE: Favorability",
      
      # original response (as character vector)
        resp_soc_dist_15_original = 
          dplyr::recode(
            as.character(Q30C),
            "8" = NA_character_,
            "9" = NA_character_),       

      # binary recode (numeric: 1 = any negative attitude expressed; 0 otherwise)
        resp_soc_dist_15_bin_recode = 
          case_when(
            Q30C %in% c(1:2) ~ 0,
            Q30C %in% c(3:4) ~ 1,
            TRUE ~ NA_real_),
    
    #########################
    ### SOCIAL DISTANCE 16 ##
    #########################
    
      # original question number; question text; response options (input above)
        resp_soc_dist_16_qinfo = "NUM: Q31 [Q31D]; QTEXT: I'd like you to rate some different groups of people in (survey country) according to how you feel about them. For each group, please tell me whether your opinion is very favorable, mostly favorable, mostly unfavorable or very unfavorable? Jews.; ROPTIONS: 1 = Very favorable [=0] + 2 = Mostly favorable [=0] + 3 = Mostly unfavorable [=1] + 4 = Very unfavorable [=1]; TARGET: Jewish; TYPE: Favorability",
      
      # original response (as character vector)
        resp_soc_dist_16_original = 
          dplyr::recode(
            as.character(Q31D),
            "8" = NA_character_,
            "9" = NA_character_),       

      # binary recode (numeric: 1 = any negative attitude expressed; 0 otherwise)
        resp_soc_dist_16_bin_recode = 
          case_when(
            Q31D %in% c(1:2) ~ 0,
            Q31D %in% c(3:4) ~ 1,
            TRUE ~ NA_real_),
    
    #########################
    ### SOCIAL DISTANCE 17 ##
    #########################
    
      # original question number; question text; response options (input above)
        resp_soc_dist_17_qinfo = "NUM: Q32 [Q32C]; QTEXT: I'd like you to rate some different groups of people who live in (survey country) according to how you feel about them. For each group, please tell me whether your opinion is very favorable, mostly favorable, mostly unfavorable or very unfavorable? Jews.; ROPTIONS: 1 = Very favorable [=0] + 2 = Mostly favorable [=0] + 3 = Mostly unfavorable [=1] + 4 = Very unfavorable [=1]; TARGET: Jewish; TYPE: Favorability",
      
      # original response (as character vector)
        resp_soc_dist_17_original = 
          dplyr::recode(
            as.character(Q32C),
            "8" = NA_character_,
            "9" = NA_character_),       

      # binary recode (numeric: 1 = any negative attitude expressed; 0 otherwise)
        resp_soc_dist_17_bin_recode = 
          case_when(
            Q32C %in% c(1:2) ~ 0,
            Q32C %in% c(3:4) ~ 1,
            TRUE ~ NA_real_),
    
    ############################  
    ### GENERAL SOCIAL TRUST ###  
    ############################
    
      # original question number; question text; response options (input above)
        resp_gentrust_qinfo = NA, # checked; no question asked

      # original response (as character vector)
        resp_gentrust_original = NA,       

      # binary recode (numeric: 1 = any negative attitude expressed; 0 otherwise)
        resp_gentrust_bin_recode = NA,    
    
    #########################  
    ##### RELIGIOSITY #######  
    #########################
    
      # original question number; question text; response options (input above)
        resp_religiosity_qinfo = "NUM: Q58; QTEXT: How important is religion in your life – very important, somewhat important, not too important, or not at all important?; ROPTIONS: 1 = Very important + 2 = Somewhat important + 3 = Not too important + 4 = Not at all important",
  
      # original response (as numeric vector, with non-substantive responses coded as NA_real_)
        resp_religiosity_original = 
          dplyr::recode(
            as.numeric(Q58),
            `8` = NA_real_,
            `9` = NA_real_),       

      # recode (numeric: scaled 0-1, where 1 is more religious)
        resp_religiosity_recode = (4 - resp_religiosity_original)/3
    
    ) %>% 
    select(starts_with("resp_"))
```

# Clean Pew Global Attitudes Spring 2010

```{r}
# clean pew global attitudes spring 2010
  n7_clean_pew_gap_spring_10 <-
    pew_gap_spring_10_raw %>% 
    mutate(
      
    #########################  
    ####### META-DATA #######  
    #########################      
      
      # source name (character vector, title case)
        resp_source = "Pew Global Attitudes Project",
        
      # round number (character vector, title case)  
        resp_round = "Spring 2010",
      
      # url to dataset source, where publicly available (character vector)
        resp_original_data_url = "pewrsr.ch/3wToJeP",

      # survey mode (in-person/phone/internet/mail)
        resp_survey_mode = 
          case_when(
            COUNTRY %in% c(3, 6, 7, 10, 20, 22) ~ "phone",
            TRUE ~ "in-person"), 

      # country (character vector; list of countries as written in original source)
        resp_country_original = to_character(COUNTRY),

      # country (character vector; converts to countrycode county.name list)
        resp_country_common = 
          countryname(resp_country_original),
        
      # interview date (variable of class Date; if only month given, input 1st of month)
        resp_interview_date = as.Date(as.character(Q147)),
   
    #########################  
    ##### DEMOGRAPHICS ######  
    #########################
      
      # respondent denomination (character vector from master list)
        q50usa = RELIG,
        across(starts_with("q50"), to_character)) %>% 
    mutate(
        resp_denomination = coalesce(!!! select(., starts_with("q50"))),
        
      # respondent's age (character vector; bins denoted by single dash ["18-25"])
        resp_age =
          dplyr::recode(
            as.character(Q121),
            "97" = "97+",
            "98" = NA_character_,
            "99" = NA_character_),        
      
      # respondent's education level
        resp_education_original =
          case_when(
            #Argentina
            Q129ARG == 1 ~ "1. No formal education [No education]",
            Q129ARG == 2 ~ "2. Incomplete primary school [No education]",
            Q129ARG == 3 ~ "3. Complete primary school [Primary]",
            Q129ARG == 4 ~ "4. Incomplete secondary school [Primary]",
            Q129ARG == 5 ~ "5. Complete secondary school [Primary]",
            Q129ARG == 6 ~ "6. Incomplete tertiary school [Primary]",
            Q129ARG == 7 ~ "7. Complete tertiary school [Primary]",
            Q129ARG == 8 ~ "8. Incomplete university [Primary]",
            Q129ARG == 9 ~ "9. Complete university [College]",
            Q129ARG %in% c(98, 99) ~ NA_character_,
            #Brazil
            Q129BRA == 1 ~ "1. No formal education [No education]",
            Q129BRA == 2 ~ "2. Incomplete primary school [No education]",
            Q129BRA == 3 ~ "3. Complete primary school [Primary]",
            Q129BRA == 4 ~ "4. Incomplete secondary school [Primary]",
            Q129BRA == 5 ~ "5. Complete secondary school [Primary]",
            Q129BRA == 6 ~ "6. Incomplete tertiary school [Primary]",
            Q129BRA == 7 ~ "7. Complete tertiary school [Primary]",
            Q129BRA == 8 ~ "8. Incomplete university [Primary]",
            Q129BRA == 9 ~ "9. Complete university [College]",
            Q129BRA %in% c(98, 99) ~ NA_character_,
            #Britain
            Q129BRI == 1 ~ "1. No qualifications obtained [Primary]", # compulsory edu
            Q129BRI == 2 ~ "2. GCSE or 'O' level grades D or below/CSE grades 2 or below [Primary]",
            Q129BRI == 3 ~ "3. *GCSE or 'O' level grades A-C/CSE grade 1/NVQ 2 [Primary]",
            Q129BRI == 4 ~ "4. 'A' level / NVQ 3 [Primary]",
            Q129BRI == 5 ~ "5. Degree/HND/teaching or nursing certificates/NVQ 4 [College]",
            Q129BRI == 6 ~ "6. Higher degree (masters, PHD, NVQ 5) [College]",
            Q129BRI == 7 ~ "7. Other qualification [Primary]",
            Q129BRI %in% c(98, 99) ~ NA_character_,
            #China
            Q129CHI == 97 ~ "0. No formal education [No education]",
            Q129CHI == 1 ~ "1. Below primary school [No education]",
            Q129CHI == 2 ~ "2. Primary school [Primary]",
            Q129CHI == 3 ~ "3. Junior school [Primary]",
            Q129CHI == 4 ~ "4. High school or technical secondary school [Primary]",
            Q129CHI == 5 ~ "5. College [Primary]", # must refer to a below BA qualification
            Q129CHI == 6 ~ "6. Bachelor's degree [College]",
            Q129CHI == 7 ~ "7. Master's degree [College]",
            Q129CHI == 8 ~ "8. Doctoral degree and above [College]",
            Q129CHI %in% c(98, 99) ~ NA_character_,
            #Egypt
            Q129EGY == 1 ~ "1. No formal education [No education]",
            Q129EGY == 2 ~ "2. Illiterate/Incomplete primary [No education]",
            Q129EGY == 3 ~ "3. Complete primary [Primary]",
            Q129EGY == 4 ~ "4. Incomplete intermediate [Primary]",
            Q129EGY == 5 ~ "5. Incomplete secondary [Primary]",
            Q129EGY == 6 ~ "6. Complete high school [Primary]",
            Q129EGY == 7 ~ "7. Complete College [College]",
            Q129EGY == 8 ~ "8. More than College [College]",
            Q129EGY %in% c(98, 99) ~ NA_character_,
            #France
            Q129FRA == 1 ~ "1. No formal education [No education]",
            Q129FRA == 2 ~ "2. Primary school [Primary]",
            Q129FRA == 3 ~ "3. Secondary school: technical/vocational type [Primary]",
            Q129FRA == 4 ~ "4. Secondary school: university-preparatory type [Primary]",
            Q129FRA == 5 ~ "5. Post-secondary school, with degree [Primary]",
            Q129FRA == 6 ~ "6. University-level education, with degree [College]",
            Q129FRA %in% c(8, 9) ~ NA_character_,
            #Germany
            Q129GER == 1 ~ "1. Still attending school [Primary]",
            Q129GER == 2 ~ "2. Lower secondary school without professional training [Primary]",
            Q129GER == 3 ~ "3. Lower secondary school with professional training [Primary]",
            Q129GER == 4 ~ "4. Middle secondary school without matriculation standard [Primary]",
            Q129GER == 5 ~ "5. General qualifications for university entrance [Primary]",
            Q129GER == 6 ~ "6. University degree [College]",
            Q129GER %in% c(8, 9) ~ NA_character_,
            #India
            Q129INDIA == 1 ~ "1. Illiterate [No education]",
            Q129INDIA == 2 ~ "2. Literate but no formal schooling [No education]",
            Q129INDIA == 3 ~ "3. School up to 4 years [No education]", # not 5 years
            Q129INDIA == 4 ~ "4. School up to 5 to 9 years [Primary]",
            Q129INDIA == 5 ~ "5. SSC/HSC [Primary]",
            Q129INDIA == 6 ~ "6. Some college but not graduated [Primary]",
            Q129INDIA == 7 ~ "7. Graduate/Post grad-Gen BA MSc BCom etc [College]",
            Q129INDIA == 8 ~ "8. Graduate/Post grad-Prof BE MTech MBA MBBS etc [College]",
            Q129INDIA %in% c(98, 99) ~ NA_character_,
            #Indonesia
            Q129INDO == 1 ~ "1. No formal education [No education]",
            Q129INDO == 2 ~ "2. Incomplete primary school [No education]",
            Q129INDO == 3 ~ "3. Complete primary school [Primary]",
            Q129INDO == 4 ~ "4. Incomplete secondary school technical/vocational type [Primary]",
            Q129INDO == 5 ~ "5. Complete secondary school technical/vocational type [Primary]",
            Q129INDO == 6 ~ "6. Incomplete secondary University-preparatory type [Primary]",
            Q129INDO == 7 ~ "7. Complete secondary University-preparatory type [Primary]",
            Q129INDO == 8 ~ "8. University-level education, without degree [Primary]",
            Q129INDO == 9 ~ "9. University-level education, with degree [College]",
            Q129INDO %in% c(98, 99) ~ NA_character_,
            #Japan
            Q129JAP == 1 ~ "1. No formal education [No education]",
            Q129JAP == 2 ~ "2. Incomplete junior high school [Primary]",
            Q129JAP == 3 ~ "3. Complete junior high school [Primary]",
            Q129JAP == 4 ~ "4. Incomplete high school [Primary]",
            Q129JAP == 5 ~ "5. Complete high school [Primary]",
            Q129JAP == 6 ~ "6. Currently in technical school [Primary]",
            Q129JAP == 7 ~ "7. Incomplete technical school [Primary]",
            Q129JAP == 8 ~ "8. Complete technical school [Primary]",
            Q129JAP == 9 ~ "9. Currently in junior College [Primary]",
            Q129JAP == 10 ~ "10. Incomplete junior College [Primary]",
            Q129JAP == 11 ~ "11. Complete junior College [Primary]",
            Q129JAP == 12 ~ "12. Currently in university [Primary]",
            Q129JAP == 13 ~ "13. Incomplete university [Primary]",
            Q129JAP == 14 ~ "14. Complete university [College]",
            Q129JAP == 15 ~ "15. Currently in post graduate [College]",
            Q129JAP == 16 ~ "16. Incomplete junior College [College]",
            Q129JAP == 17 ~ "17. Complete post graduate [College]",
            Q129JAP %in% c(98, 99) ~ NA_character_,
            #Jordan
            Q129JOR == 1 ~ "1. No formal education [No education]",
            Q129JOR == 2 ~ "2. Incomplete primary [No education]",
            Q129JOR == 3 ~ "3. Complete primary [Primary]",
            Q129JOR == 4 ~ "4. Incomplete secondary (vocational) [Primary]",
            Q129JOR == 5 ~ "5. Complete secondary (vocational) [Primary]",
            Q129JOR == 6 ~ "6. Incomplete secondary (preparatory) [Primary]",
            Q129JOR == 7 ~ "7. Complete secondary (preparatory) [Primary]",
            Q129JOR == 8 ~ "8. Some university without degree [Primary]",
            Q129JOR == 9 ~ "9. University with degree [College]",
            Q129JOR %in% c(98, 99) ~ NA_character_,
            #Kenya
            Q129KEN == 1 ~ "1. No formal education [No education]",
            Q129KEN == 2 ~ "2. Incomplete primary school [No education]",
            Q129KEN == 3 ~ "3. Complete primary school [Primary]",
            Q129KEN == 4 ~ "4. Incomplete secondary school [Primary]",
            Q129KEN == 5 ~ "5. Complete secondary school [Primary]",
            Q129KEN == 6 ~ "6. Some college (Diploma/Certificate) [Primary]",
            Q129KEN == 7 ~ "7. Completed college (Diploma/Certificate) [Primary]", # must refer to below-BA
            Q129KEN == 8 ~ "8. Some university -level education, without a degree [Primary]",
            Q129KEN == 9 ~ "9. University-level education, with degree [College]",
            Q129KEN %in% c(98, 99) ~ NA_character_,
            #Lebanon
            Q129LEB == 1 ~ "1. No formal education [No education]",
            Q129LEB == 2 ~ "2. Incomplete primary [No education]",
            Q129LEB == 3 ~ "3. Complete primary [Primary]",
            Q129LEB == 4 ~ "4. Incomplete secondary (vocational) [Primary]",
            Q129LEB == 5 ~ "5. Complete secondary (vocational) [Primary]",
            Q129LEB == 6 ~ "6. Incomplete secondary (preparatory) [Primary]",
            Q129LEB == 7 ~ "7. Complete secondary (preparatory) [Primary]",
            Q129LEB == 8 ~ "8. Some university without degree [Primary]",
            Q129LEB == 9 ~ "9. University with degree [College]",
            Q129LEB %in% c(98, 99) ~ NA_character_,
            #Mexico
            Q129MEX == 1 ~ "1. No formal education [No education]",
            Q129MEX == 2 ~ "2. Incomplete primary school [No education]",
            Q129MEX == 3 ~ "3. Complete primary school [Primary]",
            Q129MEX == 4 ~ "4. Incomplete secondary school: technical/vocational type [Primary]",
            Q129MEX == 5 ~ "5. Complete secondary school: technical/vocational type [Primary]",
            Q129MEX == 6 ~ "6. Incomplete secondary: university-preparatory type [Primary]",
            Q129MEX == 7 ~ "7. Complete secondary: university-preparatory type [Primary]",
            Q129MEX == 8 ~ "8. Some university-level education, without a degree [Primary]",
            Q129MEX == 9 ~ "9. University-level education, with degree [College]",
            Q129MEX %in% c(98, 99) ~ NA_character_,
            #Nigeria
            Q129NIG == 1 ~ "1. No formal education [No education]",
            Q129NIG == 2 ~ "2. Incomplete primary school [No education]",
            Q129NIG == 3 ~ "3. Complete primary school [Primary]",
            Q129NIG == 4 ~ "4. Incomplete secondary school: technical/vocational type [Primary]",
            Q129NIG == 5 ~ "5. Complete secondary school: technical/vocational type [Primary]",
            Q129NIG == 6 ~ "6. Incomplete secondary: university-preparatory type [Primary]",
            Q129NIG == 7 ~ "7. Complete secondary: university-preparatory type [Primary]",
            Q129NIG == 8 ~ "8. Incomplete College of Education [Primary]",
            Q129NIG == 9 ~ "9. Complete College of Education [Primary]",
            Q129NIG == 10 ~ "10. Incomplete Polytechnic [Primary]",
            Q129NIG == 11 ~ "11. Complete Polytechnic [Primary]",
            Q129NIG == 12 ~ "12. Some university-level education, without a degree [Primary]",
            Q129NIG == 13 ~ "13. University-level education, with degree [College]",
            Q129NIG %in% c(14, 15) ~ NA_character_,
            #Pakistan
            Q129PAK == 1 ~ "1. No formal education [No education]",
            Q129PAK == 2 ~ "2. No formal education but can read and write [No education]",
            Q129PAK == 3 ~ "3. Less than 5 classes [No education]", # not 5 years
            Q129PAK == 4 ~ "4. School 5-9 Years [Primary]",
            Q129PAK == 5 ~ "5. Matriculation [Primary]",
            Q129PAK == 6 ~ "6. Intermediate [Primary]",
            Q129PAK == 7 ~ "7. Graduate [College]",
            Q129PAK == 8 ~ "8. Post Graduate [College]",
            Q129PAK %in% c(98, 99) ~ NA_character_,
            #Poland
            Q129POL == 1 ~ "1. No formal education [No education]",
            Q129POL == 2 ~ "2. Incomplete primary school [No education]",
            Q129POL == 3 ~ "3. Complete primary school [Primary]",
            Q129POL == 4 ~ "4. Vocational school (2-3 years) [Primary]",
            Q129POL == 5 ~ "5. Complete secondary school [Primary]",
            Q129POL == 6 ~ "6. Incomplete post secondary school [Primary]",
            Q129POL == 7 ~ "7. Complete post secondary school [Primary]",
            Q129POL == 8 ~ "8. Some university-level education, without a diploma [Primary]",
            Q129POL == 9 ~ "9. University-level education, with diploma [College]",
            Q129POL %in% c(98, 99) ~ NA_character_,
            #Russia
            Q129RUS == 1 ~ "1. No formal education [No education]",
            Q129RUS == 2 ~ "2. Primary education (up to 4 classes, inclusive) [Primary]", # indicates complete primary, even if not 5 years
            Q129RUS == 3 ~ "3. Secondary Education (up to 8-9 classes) [Primary]",
            Q129RUS == 4 ~ "4. Secondary school (high school or vocational school) [Primary]",
            Q129RUS == 5 ~ "5. Secondary education: College [Primary]",
            Q129RUS == 6 ~ "6. Incomplete higher education (at least 3 courses) [Primary]",
            Q129RUS == 7 ~ "7. Completed higher education [College]",
            Q129RUS %in% c(98, 99) ~ NA_character_,
            #South Korea
            Q129SKOR == 1 ~ "1. No formal education [No education]",
            Q129SKOR == 2 ~ "2. Incomplete elementary school [No education]",
            Q129SKOR == 3 ~ "3. Complete elementary school [Primary]",
            Q129SKOR == 4 ~ "4. Incomplete Junior High school [Primary]",
            Q129SKOR == 5 ~ "5. Complete Junior High school [Primary]",
            Q129SKOR == 6 ~ "6. Incomplete high school [Primary]",
            Q129SKOR == 7 ~ "7. Complete high school [Primary]",
            Q129SKOR == 8 ~ "8. Incomplete College/University or Some College [Primary]",
            Q129SKOR == 9 ~ "9. Complete College/University [College]",
            Q129SKOR %in% c(98, 99) ~ NA_character_,
            #Spain
            Q129SPA == 1 ~ "1. No formal education [No education]",
            Q129SPA == 2 ~ "2. Primary studies incomplete [No education]",
            Q129SPA == 3 ~ "3. Primary studies complete (EGB 1) [Primary]",
            Q129SPA == 4 ~ "4. Secondary studies - stage 1 (EGB 2) [Primary]",
            Q129SPA == 5 ~ "5. Secondary studies - stage 2 [Primary]",
            Q129SPA == 6 ~ "6. Tertiary studies - College [College]", #assume first stage
            Q129SPA == 7 ~ "7. Tertiary studies - university [College]",
            Q129SPA %in% c(8, 9) ~ NA_character_,
            #Turkey
            Q129TUR == 1 ~ "1. No formal education [No education]",
            Q129TUR == 2 ~ "2. Incomplete primary school [No education]",
            Q129TUR == 3 ~ "3. Complete primary school [Primary]",
            Q129TUR == 4 ~ "4. Incomplete secondary school: technical/vocational type [Primary]",
            Q129TUR == 5 ~ "5. Complete secondary school: technical/vocational type [Primary]",
            Q129TUR == 6 ~ "6. Incomplete secondary: university-preparatory type [Primary]",
            Q129TUR == 7 ~ "7. Complete secondary: university-preparatory type [Primary]",
            Q129TUR == 8 ~ "8. Some university-level education, without a degree [Primary]",
            Q129TUR == 9 ~ "9. University-level education, with degree [College]",
            Q129TUR %in% c(98, 99) ~ NA_character_,
            #US
            Q129US == 1 ~ "1. None, or grade 1-8 [Primary]",
            Q129US == 2 ~ "2. High school incomplete (Grades 9-11) [Primary]",
            Q129US == 3 ~ "3. High school graduate (Grade 12 or GED certificate) [Primary]",
            Q129US == 4 ~ "4. Technical, trade, or vocational school AFTER high school [Primary]",
            Q129US == 5 ~ "5. Some college, no 4-year degree (including associate degree) [Primary]",
            Q129US == 6 ~ "6. College graduate (B.S., B.A., or other 4-year degree) [College]",
            Q129US == 7 ~ "7. Post-graduate training or professional schooling after college (e.g., toward a master's Degree or Ph.D., law or medical [College]",
            Q129US == 9 ~ NA_character_),       
      
      # respondent's gender (numeric: female = 1; male = 0; other = NA)
        resp_female =
          case_when(
            Q120 == 1 ~ 0,
            Q120 == 2 ~ 1),
      
      # respondent resident in rural (vs urban) area (numeric: rural = 1; urban/semi-urban/peri-urban = 0)
        resp_rural =
          case_when(
            Q145BRI == 4|
            Q145CHI == 3|
            Q145FRA == 1|
            Q145GER == 3|
            Q145JAP == 2|
            Q145MEX == 2|
            Q145NIG == 3|
            Q145SKOR == 3|
            Q145SPA == 3|
            Q145UR == 2|
            Q145USR == 3|
            Q145USR == 3 ~ 1, #rural
            
            Q145BRI %in% c(1:3)|
            Q145CHI %in% c(1:2)|
            Q145FRA %in% c(2:5)|
            Q145GER %in% c(1:2)|
            Q145JAP == 1|
            Q145MEX %in% c(1, 3)|
            Q145NIG %in% c(1:2)|
            Q145SKOR %in% c(1:2)|
            Q145SPA %in% c(1:2)|
            Q145UR == 1|
            Q145USR %in% c(1:2)|
            Q145USR %in% c(1:2) ~ 0, #urban
            
            TRUE ~ NA_real_),
      
    #########################  
    ### SOCIAL DISTANCE 1 ###  
    #########################
    
      # original question number; question text; response options (input above)
        resp_soc_dist_1_qinfo = "NUM: Q7 [Q7G]; QTEXT: Please tell me if you have a very favorable, somewhat favorable, somewhat unfavorable or very unfavorable opinion of Jews?; ROPTIONS: 1 = Very favorable [=0] + 2 = Somewhat favorable [=0] + 3 = Somewhat unfavorable [=1] + 4 = Very unfavorable [=1]; TARGET: Jewish; TYPE: Favorability",
      
      # original response (as character vector)
        resp_soc_dist_1_original = 
          dplyr::recode(
            as.character(Q7G),
            "8" = NA_character_,
            "9" = NA_character_),       

      # binary recode (numeric: 1 = any negative attitude expressed; 0 otherwise)
        resp_soc_dist_1_bin_recode = 
          case_when(
            Q7G %in% c(1:2) ~ 0,
            Q7G %in% c(3:4) ~ 1,
            TRUE ~ NA_real_),
    
    #########################  
    ### SOCIAL DISTANCE 2 ###  
    #########################
    
      # original question number; question text; response options (input above)
        resp_soc_dist_2_qinfo = "NUM: Q7 [Q7H]; QTEXT: Please tell me if you have a very favorable, somewhat favorable, somewhat unfavorable or very unfavorable opinion of Christians?; ROPTIONS: 1 = Very favorable [=0] + 2 = Somewhat favorable [=0] + 3 = Somewhat unfavorable [=1] + 4 = Very unfavorable [=1]; TARGET: Christian; TYPE: Favorability",
      
      # original response (as character vector)
        resp_soc_dist_2_original = 
          dplyr::recode(
            as.character(Q7H),
            "8" = NA_character_,
            "9" = NA_character_),       

      # binary recode (numeric: 1 = any negative attitude expressed; 0 otherwise)
        resp_soc_dist_2_bin_recode = 
          case_when(
            Q7H %in% c(1:2) ~ 0,
            Q7H %in% c(3:4) ~ 1,
            TRUE ~ NA_real_),
    
    #########################  
    ### SOCIAL DISTANCE 3 ###  
    #########################
    
      # original question number; question text; response options (input above)
        resp_soc_dist_3_qinfo = "NUM: Q7 [Q7I]; QTEXT: Please tell me if you have a very favorable, somewhat favorable, somewhat unfavorable or very unfavorable opinion of Muslims?; ROPTIONS: 1 = Very favorable [=0] + 2 = Somewhat favorable [=0] + 3 = Somewhat unfavorable [=1] + 4 = Very unfavorable [=1]; TARGET: Muslim; TYPE: Favorability",
      
      # original response (as character vector)
        resp_soc_dist_3_original = 
          dplyr::recode(
            as.character(Q7I),
            "8" = NA_character_,
            "9" = NA_character_),       

      # binary recode (numeric: 1 = any negative attitude expressed; 0 otherwise)
        resp_soc_dist_3_bin_recode = 
          case_when(
            Q7I %in% c(1:2) ~ 0,
            Q7I %in% c(3:4) ~ 1,
            TRUE ~ NA_real_),
    
    ############################  
    ### GENERAL SOCIAL TRUST ###  
    ############################
    
      # original question number; question text; response options (input above)
        resp_gentrust_qinfo = NA, # checked; no question asked

      # original response (as character vector)
        resp_gentrust_original = NA,       

      # binary recode (numeric: 1 = any negative attitude expressed; 0 otherwise)
        resp_gentrust_bin_recode = NA,    
    
    #########################  
    ##### RELIGIOSITY #######  
    #########################
    
      # original question number; question text; response options (input above)
        resp_religiosity_qinfo = "NUM: Q127; QTEXT: How important is religion in your life – very important, somewhat important, not too important, or not at all important?; ROPTIONS: 1 = Very important + 2 = Somewhat important + 3 = Not too important + 4 = Not at all important",
  
      # original response (as numeric vector, with non-substantive responses coded as NA_real_)
        resp_religiosity_original = 
          dplyr::recode(
            as.numeric(Q127),
            `8` = NA_real_,
            `9` = NA_real_),       

      # recode (numeric: scaled 0-1, where 1 is more religious)
        resp_religiosity_recode = (4 - resp_religiosity_original)/3
    
    ) %>% 
    select(starts_with("resp_"))
```

# Clean Pew Global Attitudes Project Spring 2011

```{r}
# clean pew global attitudes spring 2011
  n8_clean_pew_gap_spring_11 <-
    pew_gap_spring_11_raw %>% 
    mutate(
      
    #########################  
    ####### META-DATA #######  
    #########################      
      
      # source name (character vector, title case)
        resp_source = "Pew Global Attitudes Project",
        
      # round number (character vector, title case)  
        resp_round = "Spring 2011",
      
      # url to dataset source, where publicly available (character vector)
        resp_original_data_url = "pewrsr.ch/3wToJeP",

      # survey mode (in-person/phone/internet/mail)
        resp_survey_mode = 
          case_when(
            COUNTRY %in% c(2, 5, 6, 10, 20, 23) ~ "phone",
            TRUE ~ "in-person"),

      # country (character vector; list of countries as written in original source)
        resp_country_original = to_character(COUNTRY),
        resp_country_original = 
          case_when(
            resp_country_original == "Pakistan refield" ~ "Pakistan",
            TRUE ~ resp_country_original),
    

      # country (character vector; converts to countrycode county.name list)
        resp_country_common = 
          countryname(resp_country_original),
        
      # interview date (variable of class Date; if only month given, input 1st of month)
        resp_interview_date = as.Date(as.character(QDATE)),
   
    #########################  
    ##### DEMOGRAPHICS ######  
    #########################

    # respondent denomination (character vector from master list)
        q34usa = RELIG, #usa treated separately
        across(starts_with("q34"), to_character)) %>% 
    mutate(
        resp_denomination = coalesce(!!! select(., starts_with("q34"))),

      # respondent's age (character vector; bins denoted by single dash ["18-25"])
        resp_age =
          dplyr::recode(
            as.character(Q112),
            "97" = "97+",
            "98" = NA_character_,
            "99" = NA_character_),        
      
      # respondent's education level
        resp_education_original =
          case_when(
            #Brazil
            Q120BRA == 1 ~ "1. No formal education [No education]",
            Q120BRA == 2 ~ "2. Incomplete primary school [No education]",
            Q120BRA == 3 ~ "3. Complete primary school [Primary]",
            Q120BRA == 4 ~ "4. Incomplete secondary school [Primary]",
            Q120BRA == 5 ~ "5. Complete secondary school [Primary]",
            Q120BRA == 6 ~ "6. Incomplete tertiary school [Primary]",
            Q120BRA == 7 ~ "7. Complete tertiary school [Primary]",
            Q120BRA == 8 ~ "8. Incomplete university [Primary]",
            Q120BRA == 9 ~ "9. Complete university [College]",
            Q120BRA == 10 ~ "10. Post university degree (Master, post-grad, PhD, etc) [College]",
            Q120BRA %in% c(98, 99) ~ NA_character_,
            #Britain
            Q120BRI == 1 ~ "1. No qualifications obtained [Primary]", # compulsory edu
            Q120BRI == 2 ~ "2. GCSE or 'O' level grades D or below/CSE grades 2 or below [Primary]",
            Q120BRI == 3 ~ "3. *GCSE or 'O' level grades A-C/CSE grade 1/NVQ 2 [Primary]",
            Q120BRI == 4 ~ "4. 'A' level / NVQ 3 [Primary]",
            Q120BRI == 5 ~ "5. Degree/HND/teaching or nursing certificates/NVQ 4 [College]",
            Q120BRI == 6 ~ "6. Higher degree (masters, PHD, NVQ 5) [College]",
            Q120BRI == 7 ~ "7. Other qualification [Primary]",
            Q120BRI %in% c(98, 99) ~ NA_character_,
            #China
            Q120CHI == 97 ~ "0. No formal education [No education]",
            Q120CHI == 1 ~ "1. Below primary school [No education]",
            Q120CHI == 2 ~ "2. Primary school [Primary]",
            Q120CHI == 3 ~ "3. Junior school [Primary]",
            Q120CHI == 4 ~ "4. High school or technical secondary school [Primary]",
            Q120CHI == 5 ~ "5. College [Primary]",
            Q120CHI == 6 ~ "6. Bachelor's degree [College]",
            Q120CHI == 7 ~ "7. Master's degree [College]",
            Q120CHI == 8 ~ "8. Doctoral degree and above [College]",
            Q120CHI %in% c(98, 99) ~ NA_character_,
            #Egypt
            Q120EGY == 1 ~ "1. No formal education [No education]",
            Q120EGY == 2 ~ "2. Incomplete elementary [No education]",
            Q120EGY == 3 ~ "3. Completed elementary [Primary]",
            Q120EGY == 4 ~ "4. Complete Intermediate [Primary]",
            Q120EGY == 5 ~ "5. Complete secondary [Primary]",
            Q120EGY == 6 ~ "6. Completed college/post-secondary [Primary]",
            Q120EGY == 7 ~ "7. Completed university/Masters/Post Graduate/ PhD [College]",
            Q120EGY %in% c(98, 99) ~ NA_character_,
            #France
            Q120FRA == 1 ~ "1. No formal education [No education]",
            Q120FRA == 2 ~ "2. Primary school [Primary]",
            Q120FRA == 3 ~ "3. Secondary school: technical/vocational type [Primary]",
            Q120FRA == 4 ~ "4. Secondary school: university-preparatory type [Primary]",
            Q120FRA == 5 ~ "5. Post-secondary school, with degree [Primary]",
            Q120FRA == 6 ~ "6. University-level education, with degree [College]",
            Q120FRA %in% c(8, 9) ~ NA_character_,
            #Germany
            Q120GER == 1 ~ "1. Still attending school [Primary]",
            Q120GER == 2 ~ "2. Lower secondary school without professional training [Primary]",
            Q120GER == 3 ~ "3. Lower secondary school with professional training [Primary]",
            Q120GER == 4 ~ "4. Middle secondary school without matriculation standard [Primary]",
            Q120GER == 5 ~ "5. General qualifications for university entrance [Primary]",
            Q120GER == 6 ~ "6. University degree [College]",
            Q120GER %in% c(8, 9) ~ NA_character_,
            #India
            Q120INDIA == 1 ~ "1. Illiterate [No education]",
            Q120INDIA == 2 ~ "2. Literate but no formal schooling [No education]",
            Q120INDIA == 3 ~ "3. School up to 4 years [No education]",
            Q120INDIA == 4 ~ "4. School up to 5 to 9 years [Primary]",
            Q120INDIA == 5 ~ "5. SSC/HSC [Primary]",
            Q120INDIA == 6 ~ "6. Some college but not graduated [Primary]",
            Q120INDIA == 7 ~ "7. Graduate/Post grad-Gen BA MSc BCom etc [College]",
            Q120INDIA == 8 ~ "8. Graduate/Post grad-Prof BE MTech MBA MBBS etc [College]",
            Q120INDIA %in% c(98, 99) ~ NA_character_,
            #Indonesia
            Q120INDO == 1 ~ "1. No formal education [No education]",
            Q120INDO == 2 ~ "2. Incomplete primary school [No education]",
            Q120INDO == 3 ~ "3. Complete primary school [Primary]",
            Q120INDO == 4 ~ "4. Incomplete secondary school technical/vocational type [Primary]",
            Q120INDO == 5 ~ "5. Complete secondary school technical/vocational type [Primary]",
            Q120INDO == 6 ~ "6. Incomplete secondary University-preparatory type [Primary]",
            Q120INDO == 7 ~ "7. Complete secondary University-preparatory type [Primary]",
            Q120INDO == 8 ~ "8. University-level education, without degree [Primary]",
            Q120INDO == 9 ~ "9. University-level education, with degree [College]",
            Q120INDO %in% c(98, 99) ~ NA_character_,
            #Israel
            Q120ISR == 1 ~ "1. No formal education [No education]",
            Q120ISR == 2 ~ "2. Partial elementary [No education]",
            Q120ISR == 3 ~ "3. Completed elementary [Primary]",
            Q120ISR == 4 ~ "4. Partial junior high school [Primary]",
            Q120ISR == 5 ~ "5. Completed junior high school [Primary]",
            Q120ISR == 6 ~ "6. Partial high school [Primary]",
            Q120ISR == 7 ~ "7. Completed high school [Primary]",
            Q120ISR == 8 ~ "8. Partial tertiary education [Primary]",
            Q120ISR == 9 ~ "9. Complete tertiary-Professional (e.g., technicians, engineers, nurses, etc.) [Primary]", # not academic degree
            Q120ISR == 10 ~ "10. Academic education  (B.A. M.A. Ph.D.) [College]",
            Q120ISR %in% c(98, 99) ~ NA_character_,
            #Japan
            Q120JAP == 1 ~ "1. No formal education [No education]",
            Q120JAP == 2 ~ "2. Incomplete junior high school [Primary]",
            Q120JAP == 3 ~ "3. Complete junior high school [Primary]",
            Q120JAP == 4 ~ "4. Incomplete high school [Primary]",
            Q120JAP == 5 ~ "5. Complete high school [Primary]",
            Q120JAP == 6 ~ "6. Currently in technical school [Primary]",
            Q120JAP == 7 ~ "7. Incomplete technical school [Primary]",
            Q120JAP == 8 ~ "8. Complete technical school [Primary]",
            Q120JAP == 9 ~ "9. Currently in junior College [Primary]",
            Q120JAP == 10 ~ "10. Incomplete junior College [Primary]",
            Q120JAP == 11 ~ "11. Complete junior College [Primary]",
            Q120JAP == 12 ~ "12. Currently in university [Primary]",
            Q120JAP == 13 ~ "13. Incomplete university [Primary]",
            Q120JAP == 14 ~ "14. Complete university [College]",
            Q120JAP == 15 ~ "15. Currently in post graduate [College]",
            Q120JAP == 16 ~ "16. Incomplete post graduate [College]",
            Q120JAP == 17 ~ "17. Complete post graduate [College]",
            Q120JAP %in% c(98, 99) ~ NA_character_,
            #Jordan
            Q120JOR == 1 ~ "1. No formal education [No education]",
            Q120JOR == 2 ~ "2. Incomplete primary [No education]",
            Q120JOR == 3 ~ "3. Complete primary [Primary]",
            Q120JOR == 4 ~ "4. Incomplete secondary (vocational) [Primary]",
            Q120JOR == 5 ~ "5. Complete secondary (vocational) [Primary]",
            Q120JOR == 6 ~ "6. Incomplete secondary (preparatory) [Primary]",
            Q120JOR == 7 ~ "7. Complete secondary (preparatory) [Primary]",
            Q120JOR == 8 ~ "8. Some university without degree [Primary]",
            Q120JOR == 9 ~ "9. University with degree [College]",
            Q120JOR %in% c(98, 99) ~ NA_character_,
            #Kenya
            Q120KEN == 1 ~ "1. No formal education [No education]",
            Q120KEN == 2 ~ "2. Incomplete primary school [No education]",
            Q120KEN == 3 ~ "3. Complete primary school [Primary]",
            Q120KEN == 4 ~ "4. Incomplete secondary school [Primary]",
            Q120KEN == 5 ~ "5. Complete secondary school [Primary]",
            Q120KEN == 6 ~ "6. Some college (Diploma/Certificate) [Primary]",
            Q120KEN == 7 ~ "7. Completed college (Diploma/Certificate) [Primary]",
            Q120KEN == 8 ~ "8. Some university -level education, without a degree [Primary]",
            Q120KEN == 9 ~ "9. University-level education, with degree [College]",
            Q120KEN %in% c(98, 99) ~ NA_character_,
            #Lebanon
            Q120LEB == 1 ~ "1. No formal education [No education]",
            Q120LEB == 2 ~ "2. Incomplete primary [No education]",
            Q120LEB == 3 ~ "3. Complete primary [Primary]",
            Q120LEB == 4 ~ "4. Incomplete secondary (vocational) [Primary]",
            Q120LEB == 5 ~ "5. Complete secondary (vocational) [Primary]",
            Q120LEB == 6 ~ "6. Incomplete secondary (preparatory) [Primary]",
            Q120LEB == 7 ~ "7. Complete secondary (preparatory) [Primary]",
            Q120LEB == 8 ~ "8. Some university without degree [Primary]",
            Q120LEB == 9 ~ "9. University with degree [College]",
            Q120LEB %in% c(98, 99) ~ NA_character_,
            #Lithuania
            Q120LIT == 0 ~ "0. Not completed primary [No education]",
            Q120LIT == 1 ~ "1. Primary [Primary]",
            Q120LIT == 2 ~ "2. Basic [Primary]",
            Q120LIT == 3 ~ "3. Secondary [Primary]",
            Q120LIT == 4 ~ "4. Vocational school [Primary]",
            Q120LIT == 5 ~ "5. Higher non-university (vocational college) [Primary]",
            Q120LIT == 6 ~ "6. Higher university (university, academy) [College]",
            Q120LIT == 7 ~ "7. Doctoral studies [College]",
            Q120LIT %in% c(98, 99) ~ NA_character_,
            #Mexico
            Q120MEX == 1 ~ "1. No formal education [No education]",
            Q120MEX == 2 ~ "2. Incomplete primary school [No education]",
            Q120MEX == 3 ~ "3. Complete primary school [Primary]",
            Q120MEX == 4 ~ "4. Incomplete secondary school: technical/vocational type [Primary]",
            Q120MEX == 5 ~ "5. Complete secondary school: technical/vocational type [Primary]",
            Q120MEX == 6 ~ "6. Incomplete secondary: university-preparatory type [Primary]",
            Q120MEX == 7 ~ "7. Complete secondary: university-preparatory type [Primary]",
            Q120MEX == 8 ~ "8. Some university-level education, without a degree [Primary]",
            Q120MEX == 9 ~ "9. University-level education, with degree [College]",
            Q120MEX %in% c(98, 99) ~ NA_character_,
            #Pakistan
            Q120PAK == 1 ~ "1. No formal education [No education]",
            Q120PAK == 2 ~ "2. No formal education but can read and write [No education]",
            Q120PAK == 3 ~ "3. Less than 5 classes [No education]",
            Q120PAK == 4 ~ "4. School 5-9 Years [Primary]",
            Q120PAK == 5 ~ "5. Matriculation  [Primary]",
            Q120PAK == 6 ~ "6. Intermediate [Primary]",
            Q120PAK == 7 ~ "7. Graduate [College]",
            Q120PAK == 8 ~ "8. Post Graduate [College]",
            Q120PAK %in% c(98, 99) ~ NA_character_,
            #Palestine
            Q120PAL == 1 ~ "1. No formal education (illiterate) [No education]",
            Q120PAL == 2 ~ "2. Have not finished elementary school [No education]",
            Q120PAL == 3 ~ "3. Have finished elementary school [Primary]",
            Q120PAL == 4 ~ "4. Have not finished preparatory school [Primary]",
            Q120PAL == 5 ~ "5. Have finished preparatory school [Primary]",
            Q120PAL == 6 ~ "6. Have not finished secondary school- preparation for university studies [Primary]",
            Q120PAL == 7 ~ "7. Have finished secondary school-preparation for university studies [Primary]",
            Q120PAL == 8 ~ "8. Enrolled university but have not obtained a degree [Primary]",
            Q120PAL == 9 ~ "9. Enrolled a university and have obtained a degree [College]",
            Q120PAL == 10 ~ "10. Obtained a Master degree [College]",
            Q120PAL == 11 ~ "11. Obtained a Ph.D [College]",
            Q120PAL %in% c(98, 99) ~ NA_character_,
            #Poland
            Q120POL == 1 ~ "1. No formal education [No education]",
            Q120POL == 2 ~ "2. Incomplete primary school [No education]",
            Q120POL == 3 ~ "3. Complete primary school [Primary]",
            Q120POL == 4 ~ "4. Vocational school (2-3 years) [Primary]",
            Q120POL == 5 ~ "5. Complete secondary school [Primary]",
            Q120POL == 6 ~ "6. Incomplete post secondary school [Primary]",
            Q120POL == 7 ~ "7. Complete post secondary school [Primary]",
            Q120POL == 8 ~ "8. Some university-level education, without a diploma [Primary]",
            Q120POL == 9 ~ "9. University-level education, with diploma [College]",
            Q120POL %in% c(98, 99) ~ NA_character_,
            #Russia
            Q120RUS == 1 ~ "1. No formal education [No education]",
            Q120RUS == 2 ~ "2. Primary education (up to 4 classes, inclusive) [Primary]",
            Q120RUS == 3 ~ "3. Secondary Education (up to 8-9 classes) [Primary]",
            Q120RUS == 4 ~ "4. Secondary school (high school or vocational school) [Primary]",
            Q120RUS == 5 ~ "5. Secondary education: College [Primary]",
            Q120RUS == 6 ~ "6. Incomplete higher education (at least 3 courses) [Primary]",
            Q120RUS == 7 ~ "7. Completed higher education [College]",
            Q120RUS %in% c(98, 99) ~ NA_character_,
            #Spain
            Q120SPA == 1 ~ "1. No formal education / Illiterate [No education]",
            Q120SPA == 2 ~ "2. No formal education / literate [No education]",
            Q120SPA == 3 ~ "3. Primary studies incomplete [No education]",
            Q120SPA == 4 ~ "4. Primary studies complete (EGB 1—10 years old) [Primary]",
            Q120SPA == 5 ~ "5. Secondary studies – stage 1 (EGB 2—14 years old) [Primary]",
            Q120SPA == 6 ~ "6. Secondary studies – stage 2 (18 years old) [Primary]",
            Q120SPA == 7 ~ "7. Tertiary studies –1st degree (lower level university studies and equivalent) [College]",
            Q120SPA == 8 ~ "8. Tertiary studies –2nd degree (upper level university studies, master’s degree etc.) [College]",
            Q120SPA %in% c(8, 9) ~ NA_character_,
            #Turkey
            Q120TUR == 1 ~ "1. No formal education [No education]",
            Q120TUR == 2 ~ "2. Incomplete primary school [No education]",
            Q120TUR == 3 ~ "3. Complete primary school [Primary]",
            Q120TUR == 4 ~ "4. Incomplete secondary school: technical/vocational type [Primary]",
            Q120TUR == 5 ~ "5. Complete secondary school: technical/vocational type [Primary]",
            Q120TUR == 6 ~ "6. Incomplete secondary: university-preparatory type [Primary]",
            Q120TUR == 7 ~ "7. Complete secondary: university-preparatory type [Primary]",
            Q120TUR == 8 ~ "8. Some university-level education, without a degree [Primary]",
            Q120TUR == 9 ~ "9. University-level education, with degree [College]",
            Q120TUR %in% c(98, 99) ~ NA_character_,
            #Ukraine
            Q120UKR == 1 ~ "1. No formal education [No education]",
            Q120UKR == 2 ~ "2. Incomplete primary education [No education]",
            Q120UKR == 3 ~ "3. Primary education [Primary]",
            Q120UKR == 4 ~ "4. Incomplete secondary education [Primary]",
            Q120UKR == 5 ~ "5. Secondary education [Primary]",
            Q120UKR == 6 ~ "6. Vocational education [Primary]",
            Q120UKR == 7 ~ "7. Incomplete vocational education [Primary]",
            Q120UKR == 8 ~ "8. Incomplete higher education [Primary]",
            Q120UKR == 9 ~ "9. Higher education [College]", # presume this means completed BA
            Q120UKR == 10 ~ "10. Degree (Candidate of science \\ Doctor) [College]",
            Q120UKR %in% c(98, 99) ~ NA_character_,
            #US
            Q120US == 1 ~ "1. None, or grade 1-8 [Primary]",
            Q120US == 2 ~ "2. High school incomplete (Grades 9-11) [Primary]",
            Q120US == 3 ~ "3. High school graduate (Grade 12 or GED certificate) [Primary]",
            Q120US == 4 ~ "4. Technical, trade, or vocational school AFTER high school [Primary]",
            Q120US == 5 ~ "5. Some college, no 4-year degree (including associate degree) [Primary]",
            Q120US == 6 ~ "6. College graduate (B.S., B.A., or other 4-year degree) [College]",
            Q120US == 7 ~ "7. Post-graduate training or professional schooling after college (e.g., toward a master's Degree or Ph.D., law or medical [College]",
            Q120US %in% c(8, 9) ~ NA_character_),       
      
      # respondent's gender (numeric: female = 1; male = 0; other = NA)
        resp_female =
          case_when(
            Q111 == 1 ~ 0,
            Q111 == 2 ~ 1),
      
      # respondent resident in rural (vs urban) area (numeric: rural = 1; urban/semi-urban/peri-urban = 0)
        resp_rural = # no sign of a variable for united states
          case_when(
            
            Q140UR == 2| #codes most countries
            Q140BRI == 4|
            Q140CHI == 3|
            Q140FRA == 1|
            Q140GER == 3|
            Q140JAP == 2|
            Q140MEX == 2|
            Q140PAL == 2|
            Q140SPA == 3 ~ 1, #rural
 
            Q140UR == 1| #codes most countries
            Q140BRI %in% c(1:3)|
            Q140CHI %in% c(1:2)|
            Q140FRA %in% c(2:5)|
            Q140GER %in% c(1:2)|
            Q140JAP == 1|
            Q140MEX %in% c(1, 3)|
            Q140PAL %in% c(1, 3)|
            Q140SPA %in% c(1:2) ~ 0, #urban
            
            TRUE ~ NA_real_),
      
    #########################  
    ### SOCIAL DISTANCE 1 ###  
    #########################
    
      # original question number; question text; response options (input above)
        resp_soc_dist_1_qinfo = "NUM: Q3 [Q3G]; QTEXT: Please tell me if you have a very favorable, somewhat favorable, somewhat unfavorable or very unfavorable opinion of Jews?; ROPTIONS: 1 = Very favorable [=0] + 2 = Somewhat favorable [=0] + 3 = Somewhat unfavorable [=1] + 4 = Very unfavorable [=1]; TARGET: Jewish; TYPE: Favorability",
      
      # original response (as character vector)
        resp_soc_dist_1_original = 
          dplyr::recode(
            as.character(Q3G),
            "8" = NA_character_,
            "9" = NA_character_),       

      # binary recode (numeric: 1 = any negative attitude expressed; 0 otherwise)
        resp_soc_dist_1_bin_recode = 
          case_when(
            Q3G %in% c(1:2) ~ 0,
            Q3G %in% c(3:4) ~ 1,
            TRUE ~ NA_real_),
    
    #########################  
    ### SOCIAL DISTANCE 2 ###  
    #########################
    
      # original question number; question text; response options (input above)
        resp_soc_dist_2_qinfo = "NUM: Q3 [Q3H]; QTEXT: Please tell me if you have a very favorable, somewhat favorable, somewhat unfavorable or very unfavorable opinion of Christians?; ROPTIONS: 1 = Very favorable [=0] + 2 = Somewhat favorable [=0] + 3 = Somewhat unfavorable [=1] + 4 = Very unfavorable [=1]; TARGET: Christian; TYPE: Favorability",
      
      # original response (as character vector)
        resp_soc_dist_2_original = 
          dplyr::recode(
            as.character(Q3H),
            "8" = NA_character_,
            "9" = NA_character_),       

      # binary recode (numeric: 1 = any negative attitude expressed; 0 otherwise)
        resp_soc_dist_2_bin_recode = 
          case_when(
            Q3H %in% c(1:2) ~ 0,
            Q3H %in% c(3:4) ~ 1,
            TRUE ~ NA_real_),
    
    #########################  
    ### SOCIAL DISTANCE 3 ###  
    #########################
    
      # original question number; question text; response options (input above)
        resp_soc_dist_3_qinfo = "NUM: Q3 [Q3I]; QTEXT: Please tell me if you have a very favorable, somewhat favorable, somewhat unfavorable or very unfavorable opinion of Muslims?; ROPTIONS: 1 = Very favorable [=0] + 2 = Somewhat favorable [=0] + 3 = Somewhat unfavorable [=1] + 4 = Very unfavorable [=1]; TARGET: Muslim; TYPE: Favorability",
      
      # original response (as character vector)
        resp_soc_dist_3_original = 
          dplyr::recode(
            as.character(Q3I),
            "8" = NA_character_,
            "9" = NA_character_),       

      # binary recode (numeric: 1 = any negative attitude expressed; 0 otherwise)
        resp_soc_dist_3_bin_recode = 
          case_when(
            Q3I %in% c(1:2) ~ 0,
            Q3I %in% c(3:4) ~ 1,
            TRUE ~ NA_real_),
    
    ############################  
    ### GENERAL SOCIAL TRUST ###  
    ############################
    
      # original question number; question text; response options (input above)
        resp_gentrust_qinfo = NA, # checked; no question asked

      # original response (as character vector)
        resp_gentrust_original = NA,       

      # binary recode (numeric: 1 = any negative attitude expressed; 0 otherwise)
        resp_gentrust_bin_recode = NA,    
    
    #########################  
    ##### RELIGIOSITY #######  
    #########################
    
      # original question number; question text; response options (input above)
        resp_religiosity_qinfo = "NUM: Q118; QTEXT: How important is religion in your life – very important, somewhat important, not too important, or not at all important?; ROPTIONS: 1 = Very important + 2 = Somewhat important + 3 = Not too important + 4 = Not at all important",
  
      # original response (as numeric vector, with non-substantive responses coded as NA_real_)
        resp_religiosity_original = 
          dplyr::recode(
            as.numeric(Q118),
            `8` = NA_real_,
            `9` = NA_real_),       

      # recode (numeric: scaled 0-1, where 1 is more religious)
        resp_religiosity_recode = (4 - resp_religiosity_original)/3
    
    ) %>% 
    select(starts_with("resp_"))
```

# Clean Pew Global Attitudes Project Spring 2014

```{r}
# get the lists of countries for survey modality from Q2b 
  ls_phone_countries_2014 <-
    pew_gap_spring_14_raw %>%
    filter(Q2B > 0 & Q2B <= 99) %>%
    distinct(COUNTRY)

# declare dates: see the trade report in the folder for interview periods
  dates_pew_gap_spring_14 <-
    tribble(
      ~resp_country_common, ~resp_interview_start_date, ~resp_interview_end_date,
      "Argentina", "April 17, 2014", "May 11, 2014",
      "Bangladesh", "April 14, 2014", "May 11, 2014",
      "Brazil", "April 10, 2014", "April 30, 2014",
      "Chile", "April 25, 2014", " May 5, 2014",
      "China", "April 11, 2014", "May 15, 2014",
      "Colombia", "April 12, 2014", "May 8, 2014",
      "Egypt", "April 10, 2014", "April 29, 2014",
      "El Salvador", "April 28, 2014", "May 9, 2014",
      "France", "March 17, 2014", "April 1, 2014",
      "Germany", "March 17, 2014", "April 2, 2014",
      "Ghana", "May 5, 2014", "May 31, 2014",
      "Greece", "March 22, 2014", "April 9, 2014",
      "India", "April 14, 2014", "May 1, 2014",
      "Indonesia", "April 17, 2014", "May 23, 2014",
      "Israel", "April 24, 2014", "May 11, 2014",
      "Italy", "March 18, 2014", "April 7, 2014",
      "Japan", "April 10, 2014", "April 27, 2014",
      "Jordan", "April 11, 2014", "April 29, 2014",
      "Kenya", "April 18, 2014", "April 28, 2014",
      "Lebanon", "April 11, 2014", "May 2, 2014",
      "Malaysia", "April 10, 2014", "May 23, 2014",
      "Mexico", "April 21, 2014", "May 2, 2014",
      "Nicaragua", "April 23, 2014", "May 11, 2014",
      "Nigeria", "April 11, 2014", "May 25, 2014",
      "Pakistan", "April 15, 2014", "May 7, 2014",
      "Palestine", "April 15, 2014", "April 22, 2014",
      "Peru", "April 11, 2014", "May 2, 2014", 
      "Philippines", "May 1, 2014", "May 21, 2014",
      "Poland", "March 17, 2014", "April 8, 2014",
      "Russia", "April 4, 2014", "April 20, 2014",
      "Senegal", "April 17, 2014", "May 2, 2014",
      "South Africa", "May 18, 2014", "June 5, 2014",
      "South Korea", "April 17, 2014", "April 30, 2014",
      "Spain", "March 17, 2014", "March 31, 2014",
      "Tanzania", "April 18, 2014", "May 7, 2014",
      "Thailand", "April 23, 2014", "May 24, 2014",
      "Tunisia", "April 19, 2014", "May 9, 2014",
      "Turkey", "April 11, 2014", "May 16, 2014",
      "Uganda", "April 25, 2014", "May 9, 2014",
      "Ukraine", "April 5, 2014", "April 23, 2014",
      "UK", "March 17, 2014", "April 8, 2014",
      "US", "April 22, 2014", "May 11, 2014",
      "Venezuela", "April 11, 2014", "May 10, 2014",
      "Vietnam", "April 16, 2014", "May 8, 2014"
    ) %>%
    mutate(
      resp_interview_start_date = as.Date(resp_interview_start_date, "%B %d, %Y"),
      resp_interview_end_date = as.Date(resp_interview_end_date, "%B %d, %Y"))

# clean pew global attitudes spring 2014
  n9_clean_pew_gap_spring_14 <-
    pew_gap_spring_14_raw %>% 
    mutate(
      
    #########################  
    ####### META-DATA #######  
    #########################      
      
      # source name (character vector, title case)
        resp_source = "Pew Global Attitudes Project",
        
      # round number (character vector, title case)  
        resp_round = "Spring 2014",
      
      # url to dataset source, where publicly available (character vector)
        resp_original_data_url = "pewrsr.ch/3wToJeP",

      # survey mode (in-person/phone/internet/mail)
        resp_survey_mode = 
          case_when(
            COUNTRY %in% c(35, 10, 9, 42, 34, 17, 43) ~ "phone",
            TRUE ~ "in-person"),

      # country (character vector; list of countries as written in original source)
        resp_country_original = to_character(COUNTRY), 

      # country (character vector; converts to countrycode county.name list)
        resp_country_common = 
          countryname(resp_country_original),
        
      # interview date (variable of class Date; if only month given, input 1st of month)
        resp_interview_date = as.Date(as.character(Q165), "%d%m%Y")) %>%
    left_join(dates_pew_gap_spring_14, by = "resp_country_common") %>%
    mutate(
   
    #########################  
    ##### DEMOGRAPHICS ######  
    #########################
    
    # respondent's denomination (character vector)
      # note, the "specify other" cols require special treatment; each is different
      # some of the "specify other" cols are all NA, which is why not all countries appear here
    QRELUSA = RELIG,
    across(starts_with("qrel"), to_character)) %>%
    mutate(
        QRELARG = case_when(QRELARGB == "Yes" ~ "Christian", TRUE ~ QRELARG),
        QRELBRI = case_when(QRELBRIB == "Yes, Christian" ~ "Christian", TRUE ~ QRELBRI),
        QRELCHL = case_when(QRELCHLB == "Yes" ~ "Christian", TRUE ~ QRELCHL),
        QRELCOL = case_when(QRELCOLB == "Yes" ~ "Christian", TRUE ~ QRELCOL),
        QRELESA = case_when(QRELESAB == "Yes, Christian" ~ "Christian", TRUE ~ QRELESA),
        QRELFRA = case_when(QRELFRAB == "Yes, Christian" ~ "Christian", TRUE ~ QRELFRA),
        QRELGER = case_when(QRELGERB == "Yes, Christian" ~ "Christian", TRUE ~ QRELGER),
        QRELGHA = 
          case_when(
            QRELGHAB == "Christian" ~ "Christian", 
            QRELGHAB == "Muslim" ~ "Muslim", 
            TRUE ~ QRELGHA),
        QRELGRE = case_when(QRELGREB == "Yes, Christian" ~ "Christian", TRUE ~ QRELGRE),    
        QRELITA = case_when(QRELITAB == "Yes, Christian" ~ "Christian", TRUE ~ QRELITA),    
        QRELKEN = case_when(QRELKENB == "Christian" ~ "Christian", TRUE ~ QRELKEN),    
        QRELMEX = case_when(QRELMEXB == "Yes, Christian" ~ "Christian", TRUE ~ QRELMEX),    
        QRELNIC = case_when(QRELNICB == "Yes" ~ "Christian", TRUE ~ QRELNIC),    
        QRELNIG = case_when(QRELNIGB == "Christian" ~ "Christian", TRUE ~ QRELNIG),    
        QRELPER = case_when(QRELPERB == "Yes" ~ "Christian", TRUE ~ QRELPER),    
        QRELPHI = case_when(QRELPHIB == "Yes, Christian" ~ "Christian", TRUE ~ QRELPHI),    
        QRELPOL = case_when(QRELPOLB == "Yes, Christian" ~ "Christian", TRUE ~ QRELPOL),    
        QRELRUS = case_when(QRELRUSB == "Christian" ~ "Christian", TRUE ~ QRELRUS),    
        QRELRSA = case_when(QRELRSAb == "Yes" ~ "Christian", TRUE ~ QRELRSA),    
        QRELSKOR = case_when(QRELSKORB == "Buddhist" ~ "Buddhist", TRUE ~ QRELSKOR),    
        QRELSPA = case_when(QRELSPAB == "Yes, Christian" ~ "Christian", TRUE ~ QRELSPA),    
        QRELTAN = case_when(QRELTANB == "Christian" ~ "Christian", TRUE ~ QRELTAN),    
        QRELUGA = case_when(QRELUGAB == "Christian" ~ "Christian", TRUE ~ QRELUGA)
      ) %>% 
      select(-c("QRELARGB", "QRELBANB", "QRELBRAB", "QRELBRIB", "QRELCHLB", "QRELCOLB", "QRELEGYB", 
      "QRELESAB", "QRELFRAB", "QRELGERB", "QRELGHAB", "QRELGREB", "QRELINDB", "QRELINDOB",
      "QRELISRB", "QRELITAB", "QRELJPNB", "QRELJORB", "QRELKENB", "QRELLEBB", 
      "QRELMALB", "QRELMEXB", "QRELNICB", "QRELNIGB", "QRELPAKB", "QRELPALB", "QRELPERB", 
      "QRELPHIB", "QRELPOLB", "QRELRUSB", "QRELSENB", "QRELRSAb", "QRELSKORB","QRELSPAB", 
      "QRELTANB", "QRELTUNB", "QRELTURB", "QRELUGAB", "QRELUKRB", "QRELVENB", "QRELVIEB")) %>%
      mutate(
        resp_denomination = coalesce(!!! select(., starts_with("QREL"))),

      # respondent's age (character vector; bins denoted by single dash ["18-25"])
        resp_age =
          dplyr::recode(
            as.character(Q133),
            "97" = "97+",
            "98" = NA_character_,
            "99" = NA_character_),        
      
      # respondent's education level
        resp_education_original =
          case_when(
            #Argentina #In Spanish
            Q138ARG == 96 ~ "0. No degree [No education]",
            Q138ARG == 1 ~ "1. Sin educación formal/No formal education [No education]",
            Q138ARG == 2 ~ "2. Primaria incompleta/Incomplete primary [No education]",
            Q138ARG == 3 ~ "3. Primaria completa/Complete primary [Primary]",
            Q138ARG == 4 ~ "4. Secundaria incompleta/Incomplete secondary [Primary]",
            Q138ARG == 5 ~ "5. Secundaria completa/Complete secondary [Primary]",
            Q138ARG == 6 ~ "6. Secundaria de nivel superior incompleta/Incomplete upper secondary [Primary]",
            Q138ARG == 7 ~ "7. Secundaria de nivel superior completa/Complete upper secondary [Primary]",
            Q138ARG == 8 ~ "8. Cursos vocacionales iniciales, nivel técnico/ supervisor en/Initial vocational, technical/supervisory courses [Primary]",
            Q138ARG == 9 ~ "9. Terciaria incompleta/Incomplete tertiary [Primary]",
            Q138ARG == 10 ~ "10. Universitaria incompleta/Incomplete university [Primary]",
            Q138ARG == 11 ~ "11. Terciaria completa/Complete tertiary [Primary]",
            Q138ARG == 12 ~ "12. Universitaria completa/Complete university [College]",
            Q138ARG == 13 ~ "13. Maestría, cursos de posgrado/Master's, postgraduate courses [College]",
            Q138ARG == 14 ~ "14. Programas de Doctorado/Doctoral programs [College]",
            Q138ARG == 97 ~ "Continúa en el Sisstema educativo/Continue in the educational system [Primary]",
            Q138ARG %in% c(98, 99) ~ NA_character_,
            #Bangladesh
            Q138BAN == 1 ~ "1. Cannot read or write [No education]",
            Q138BAN == 2 ~ "2. Literate, no formal schooling [No education]",
            Q138BAN == 3 ~ "3. Elementary [Primary]",
            Q138BAN == 4 ~ "4. Secondary, general  (years 1-3) [Primary]",
            Q138BAN == 5 ~ "5. Secondary, general [Primary]",
            Q138BAN == 6 ~ "6. Post-secondary technical vocational programmes (SSC / Dakhil) [Primary]",
            Q138BAN == 7 ~ "7. Post-secondary general programmes [Primary]",
            Q138BAN == 8 ~ "8. Associate of Arts [Primary]",
            Q138BAN == 9 ~ "9. Bachelor [College]",
            Q138BAN == 10 ~ "10. Master [College]",
            Q138BAN == 11 ~ "11. Doctorate degree [College]",
            Q138BAN %in% c(98, 99) ~ NA_character_,
            #Brazil
            Q138BRA == 1 ~ "1. Não recebeu educação formal/No formal education [No education]",
            Q138BRA == 2 ~ "2. Primário/ Fundamental (1ª a 4ª série) incompleto/Primary/Elementary (1st to 4th grade) incomplete [No education]",
            Q138BRA == 3 ~ "3. Primário/ Fundamental (1ª a 4ª série) completo/Primary/Elementary (1st to 4th grade) complete [Primary]", # complete primary under their system
            Q138BRA == 4 ~ "4. Ginasial/ Fundamental (5ª a 8ª/9ª série) incompleto/Junior High / Elementary (5th to 8th/9th grade) incomplete [Primary]",
            Q138BRA == 5 ~ "5. Ginasial/ Fundamental (5ª a 8ª/9ª série) completo/Junior High / Elementary (5th to 8th/9th grade) complete [Primary]",
            Q138BRA == 6 ~ "6. Educação Profissional de Nível Técnico ou Formação de Professores na Modalidade Normal incompleto/Professional Education at a Technical Level or Teacher Training in the Normal Modality Incomplete [Primary]",
            Q138BRA == 7 ~ "7. Educação Profissional de Nível Técnico ou Formação de Professores na Modalidade Normal completo/Professional Education at a Technical Level or Teacher Training in the Normal Modality complete [Primary]",
            Q138BRA == 8 ~ "8. Colegial/ Ensino Médio incompleto/High school/Incomplete high school [Primary]",
            Q138BRA == 9 ~ "9. Colegial/ Ensino médio completo/High school/Completed high school [Primary]",
            Q138BRA == 10 ~ "10. Curso superior incompleto (bacharelado, licenciatura ou tecnólogo)/Incomplete higher education (bachelor's, licensure or technologist) [Primary]",
            Q138BRA == 11 ~ "11. Curso superior de bacharelado ou licenciatura completo/Complete higher education (bachelor's, licensure or technologist) [College]",
            Q138BRA == 12 ~ "12. Curso superior de tecnologia completo/Complete technology course [College]",
            Q138BRA == 13 ~ "13. Pós-graduação lato sensu completo (especialização, mestrado profissional, MBA)/Complete lato sensu postgraduate degree (specialization, professional master's, MBA) [College]",
            Q138BRA == 14 ~ "14. Mestrado completo (stricto sensu, acadêmico)/Complete Master's (stricto sensu, academic) [College]",
            Q138BRA == 15 ~ "15. Doutorado completo/Complete Doctorate [College]",
            Q138BRA == 97 ~ "16. Still in education [Primary]", 
            Q138BRA %in% c(98, 99) ~ NA_character_,
            #Britain
            Q138BRI == 1 ~ "1. No qualifications obtained [Primary]", #compulsory primary
            Q138BRI == 2 ~ "2. Primary school / infant & junior schools [Primary]",
            Q138BRI == 3 ~ "3. Secondary school [Primary]",
            Q138BRI == 4 ~ "4. GCSE / SCE Standard Grades GNVQ Intermediate SQA National Courses, NVQ1, NVQ 2 [Primary]",
            Q138BRI == 5 ~ "5. NVQ3, A Level (A2),  GNVQ Advanced, AS Level, International Baccalaureate [Primary]",
            Q138BRI == 6 ~ "6. HE Access [Primary]",
            Q138BRI == 7 ~ "7. Degree/HND/teaching or nursing certificates/NVQ 4, NVQ5 [Primary]",
            Q138BRI == 8 ~ "8. University (Bachelor) [College]",
            Q138BRI == 9 ~ "9. University (Masters degree) [College]",
            Q138BRI == 10 ~ "10. University (Doctorate, PHD) [College]",
            Q138BRI == 97 ~ "11. Still studying [Primary]",
            Q138BRI %in% c(98, 99) ~ NA_character_,
            #Chile
            Q138CHL == 1 ~ "1. Analfabeto/Illiterate [No education]",
            Q138CHL == 2 ~ "2. Ninguno, no estudio/None, I don't study [No education]",
            Q138CHL == 3 ~ "3. 1º básico (preparatoria)/1st grade (preparatory) [No education]",
            Q138CHL == 4 ~ "4. 2º básico/2nd grade [No education]",
            Q138CHL == 5 ~ "5. 3º básico/3rd grade [No education]",
            Q138CHL == 6 ~ "6. 4º básico/4th grade [No education]",
            Q138CHL == 7 ~ "7. 5º básico/5th grade [Primary]",
            Q138CHL == 8 ~ "8. 6º básico/6th grade [Primary]",
            Q138CHL == 9 ~ "9. 7º básico, primero de humanidades/7th grade, first of humanities [Primary]",
            Q138CHL == 10 ~ "10. 8º básico, segundo de humanidades/8th grade, second in humanities [Primary]",
            Q138CHL == 11 ~ "11. 1º enseñanza media, tercero de humanidades/1st high school, 3rd humanities [Primary]",
            Q138CHL == 12 ~ "12. 2º enseñanza media, cuarto de humanidades/2nd high school, fourth of humanity [Primary]",
            Q138CHL == 13 ~ "13. 3º enseñanza media, quinto de humanidades/3rd high school, fifth in humanities [Primary]",
            Q138CHL == 14 ~ "14. 4º enseñanza media, sexto de humanidades/4th high school, sixth in humanities [Primary]",
            Q138CHL == 15 ~ "15. Técnico-profesional completo/Complete technical-professional [Primary]",
            Q138CHL == 16 ~ "16. Educación universitaria, incompleta/College education, incomplete [Primary]",
            Q138CHL == 17 ~ "17. Educación universitaria, bachelor completo/University education, bachelor complete [College]",
            Q138CHL == 18 ~ "18. Educación universitaria, Master completa/University education, Master complete [College]",
            Q138CHL == 19 ~ "19. Postgrado, Master, Doctorado/Postgraduate, Master, Doctorate [College]",
            Q138CHL == 97 ~ "20. Still a student [Primary]",
            Q138CHL %in% c(98, 99) ~ NA_character_,
            #China
            Q138CHI == 1 ~ "1. No education [No education]",
            Q138CHI == 2 ~ "2. Incomplete primary education [No education]",
            Q138CHI == 3 ~ "3. Primary school [Primary]",
            Q138CHI == 4 ~ "4. Pre-vocational lower secondary [Primary]",
            Q138CHI == 5 ~ "5. Junior secondary school [Primary]",
            Q138CHI == 6 ~ "6. Secondary Vocational School/Technical secondary school [Primary]",
            Q138CHI == 7 ~ "7. Senior High School [Primary]",
            Q138CHI == 8 ~ "8. Post-secondary further education (non-tertiary, no diploma) [Primary]",
            Q138CHI == 9 ~ "9. Higher technical & vocational School/Junior College [Primary]",
            Q138CHI == 10 ~ "10. Bachelor (Undergraduate) [College]",
            Q138CHI == 11 ~ "11. Master [College]",
            Q138CHI == 12 ~ "12. Doctorate [College]",
            Q138CHI == 97 ~ "13. Still in Education [Primary]",
            Q138CHI %in% c(98, 99) ~ NA_character_,
             #Colombia
            Q138COL == 1 ~ "1. Ninguno/None [No education]",
            Q138COL == 2 ~ "2. Preescolar/Preschool [No education]",
            Q138COL == 3 ~ "3. Básica primaria/Primary basic [Primary]",
            Q138COL == 4 ~ "4. Básica secundaria/Basic high school [Primary]",
            Q138COL == 5 ~ "5. Educación especial laboral/Special labor education [Primary]",
            Q138COL == 6 ~ "6. Educación media (Bachiller)/Middle education (Baccalaureate) [Primary]",
            Q138COL == 7 ~ "7. Primera calificación de la educación terciaria (Normalista, Técnico profesional, Tecnólogo)/First qualification of tertiary education (Normalist, Professional Technician, Technologist) [Primary]",
            Q138COL == 8 ~ "8. Educación universitaria/University education [College]",
            Q138COL == 9 ~ "9. Maestría/Master's [College]",
            Q138COL == 10 ~ "10. Doctorado/Doctorate [College]",
            Q138COL == 11 ~ "11. Postgrado/especialización/Postgraduate/specialization [College]",
            Q138COL == 97 ~ "12. Still in School [Primary]",
            Q138COL %in% c(98, 99) ~ NA_character_,
            #Egypt
            Q138EGY == 1 ~ "1. Pre-primary [No education]",
            Q138EGY == 2 ~ "2. Primary [Primary]",
            Q138EGY == 3 ~ "3. Vocational school [Primary]",
            Q138EGY == 4 ~ "4. Preparatory school [Primary]",
            Q138EGY == 5 ~ "5. Technical secondary program [Primary]",
            Q138EGY == 6 ~ "6. General secondary  programme [Primary]",
            Q138EGY == 7 ~ "7. Technical programmes / Industrial, commercial & technical programmes [Primary]",
            Q138EGY == 8 ~ "8. First stage of tertiary education ( Bachelor, Master, Medical programme) [College]",
            Q138EGY == 9 ~ "9. Second stage of tertiary education (Master, Medical programme) [College]",
            Q138EGY == 10 ~ "10. Doctorate [College]",
            Q138EGY == 97 ~ "11. STILL IN EDUCATION [Primary]",
            Q138EGY %in% c(98, 99) ~ NA_character_,
            #El Salvador
            Q138ESA == 1 ~ "1. Sin estudios, Educacion Parvularia [No education]",
            Q138ESA == 2 ~ "2. Sin estudios, pero sabe leer y escribir [No education]",
            Q138ESA == 3 ~ "3. Primaria incompleta [No education]",
            Q138ESA == 4 ~ "4. Educacion basica, 1-6 (Ciclo1) // Primaria completa [Primary]",
            Q138ESA == 5 ~ "5. Secundaria incompleta [Primary]",
            Q138ESA == 6 ~ "6. Educacion basica, 7-9 (Ciclo2 & Ciclo 3) [Primary]",
            Q138ESA == 7 ~ "7. Educación media (Bachillerato) [Primary]",
            Q138ESA == 8 ~ "8. Algunos estudios universitarios, incompleta [Primary]",
            Q138ESA == 9 ~ "9. Educacion Superior Tecnologica [Primary]", 
            Q138ESA == 10 ~ "10. Educacion Superior Universitario (Bachelor) [College]",
            Q138ESA == 11 ~ "11. Educacion Superior Universitario (Master) [College]",
            Q138ESA == 12 ~ "12. Postgrado Universitario // Doctorado Completo [College]",
            Q138ESA == 97 ~ "13.  Still in education [Primary]",
            Q138ESA %in% c(98, 99) ~ NA_character_,
            #France
            Q138FRA == 1 ~ "1. Non scolarisé ou école primaire non achevée/No schooling or primary school not complete [No education]",
            Q138FRA == 2 ~ "2. Ecole primaire avec le CEP/Primary school with CEP [Primary]",
            Q138FRA == 3 ~ "3. Enseignement du premier cycle du second degré – Collège (BREVET)/Education of the first grade of the second degree - College (BREVET) [Primary]",
            Q138FRA == 4 ~ "4. Enseignement de second cycle professionnel du second degré (sous statut scolaire et apprentissage) (C.A.P, B.E.P, etc)/Education of the second grade of the second degree (under school status and apprenticeship) (C.A.P, B.E.P, etc) [Primary]",
            Q138FRA == 5 ~ "5. Enseignement de second cycle général du second degré (Baccalauréat général et technologique)/General secondary education of the second degree (General and technological baccalaureate) [Primary]",
            Q138FRA == 6 ~ "6. Enseignement des écoles sociales et autres formations (Diplôme de moniteur éducateur, éducateur technique spécialisé)/Education of social schools and other training (Diploma of educator monitor, specialized technical educator) [Primary]",
            Q138FRA == 7 ~ "7. Préparation à l'entrée d'une formation sélective de niveau 5B (Prépa entrée écoles paramédicales et sociales, classes de)/Preparation for entry into selective level 5B training (Preparation for entry to paramedical and social schools) [Primary]",
            Q138FRA == 8 ~ "8. Enseignement pré-universitaire (Diplôme de la capacité en droit, diplôme d'accès aux études universitaires)/Pre-university education (Diploma of capacity in law, diploma of access to university studies) [Primary]",
            Q138FRA == 9 ~ "9. Diplôme universitaire de technologie (DUT) // Brevet de technicien supérieur (BTS), Diplôme des métiers d'art (DMA)/University diploma in technology (DUT) // Higher technician certificate (BTS), Diploma in crafts (DMA) [Primary]",
            Q138FRA == 10 ~ "10. Enseignement de premier cycle des études universitaires (Diplôme d'études universitaires générales (DEUG), LICENCE, LICE/Undergraduate education of university studies (Diploma of general university studies (DEUG), LICENCE, LICE [College]",
            Q138FRA == 11 ~ "11. Enseignement de deuxième cycle des études universitaires (MAITRISE, Diplome (DESS), Master (LMD), Diplome Ingenieur)/Second-degree education of university studies (MASTER'S, Diplome (DESS), Master's (LMD), Diplome Ingenieur) [College]",
            Q138FRA == 12 ~ "12. Enseignement de troisième cycle des études universitaires  (Diplôme d'études approfondies (DEA), Diplome de docteur)/Postgraduate education of university studies (Diploma of advanced studies (DEA), Doctor's degree) [College]",
            Q138FRA == 97 ~ "13. Still a student [Primary]",
            Q138FRA %in% c(98, 99) ~ NA_character_,
            #Germany
            Q138GER == 1 ~ "1. kein Schulabschluss / Grundschule/no school certificate / elementary school [Primary]",
            Q138GER == 2 ~ "2. Volks- oder Hauptschule, ohne Lehre/Elementary or secondary school, without an apprenticeship [Primary]",
            Q138GER == 3 ~ "3. Volks- oder Hauptschule, mit Lehre/Elementary or secondary school, with an apprenticeship [Primary]",
            Q138GER == 4 ~ "4. Weiterführende Schule ohne Abitur, ohne Lehre/Secondary school without Abitur, without apprenticeship [Primary]",
            Q138GER == 5 ~ "5. Weiterführende Schule ohne Abitur, mit Lehre/Secondary school without Abitur, with an apprenticeship [Primary]",
            Q138GER == 6 ~ "6. Abitur, Fachhochschulreife, Hochschulreife, ohne Lehre/Abitur, advanced technical college entrance qualification, university entrance qualification, without an apprenticeship [Primary]",
            Q138GER == 7 ~ "7. Abitur, Fachhochschulreife, Hochschulreife, mit  Lehre/Abitur, technical college entrance qualification, university entrance qualification, with an apprenticeship [Primary]",
            Q138GER == 8 ~ "8. Verwaltungsfachhochschule/Administrative College [Primary]",
            Q138GER == 9 ~ "9. abgeschlossenes Bachelor-Studium/ Grundstudium/ Vordiplom/ Berufsakademie, nicht abgeschl. Haupt- bzw. Master-Studium/Completed bachelor’s degree/ basic studies/ intermediate diploma/ professional academy, not completed Main or Master's degree [College]",
            Q138GER == 10 ~ "10. abgeschlossenes Haupt- bzw. Master-Studium/Completed main or Master's degree [College]",
            Q138GER == 11 ~ "11. Promotion, Doktorat/Promotion, Doctorate [College]",
            Q138GER == 97 ~ "12. Still attending school or university [Primary]",
            Q138GER %in% c(98, 99) ~ NA_character_,
            #Ghana
            Q138GHA == 1 ~ "1. No formal education [No education]",
            Q138GHA == 2 ~ "2. Kindergarten / Incomplete primary school [No education]",
            Q138GHA == 3 ~ "3. Primary school [Primary]",
            Q138GHA == 4 ~ "4. Incomplete JSS/Middle school [Primary]",
            Q138GHA == 5 ~ "5. Junior high school (BECE) [Primary]",
            Q138GHA == 6 ~ "6. Technical and vocational education [Primary]",
            Q138GHA == 7 ~ "7. Senior high school (WASCE) [Primary]",
            Q138GHA == 8 ~ "8. Incomplete SSS/Vocational/Technical/ School [Primary]",
            Q138GHA == 9 ~ "9. Polytechnics non-tertiary programmes (DBS, BA)and professional bodies programme (certification) [Primary]",
            Q138GHA == 10 ~ "10. First stage of tertiary education (Bachelor) or Teacher training [College]",
            Q138GHA == 11 ~ "11. Master [College]",
            Q138GHA == 12 ~ "12. Secondary stage of tertiary education (PhD) [College]",
            Q138GHA == 97 ~ "13. Still in education [Primary]",
            Q138GHA %in% c(98, 99) ~ NA_character_,
            #Greece
            Q138GRE == 1 ~ "1. Illiterate /elementary school incomplete [No education]",
            Q138GRE == 2 ~ "2. Elementary school complete [Primary]",
            Q138GRE == 3 ~ "3. Junior high school (gymnasium) complete [Primary]",
            Q138GRE == 4 ~ "4. Vocational high school (EPAL)/ EPAS complete [Primary]",
            Q138GRE == 5 ~ "5. High school complete (lyceum) [Primary]",
            Q138GRE == 6 ~ "6. Vocational Training Institutes (IEK) [Primary]",
            Q138GRE == 7 ~ "7. Technical Educational Institutes (T.E.I.) [Primary]",
            Q138GRE == 8 ~ "8. Technical Educational Institutes (A.T.E.I.) [Primary]",
            Q138GRE == 9 ~ "9. University (Certificate) [College]",
            Q138GRE == 10 ~ "10. University (Master) [College]",
            Q138GRE == 11 ~ "11. University (Phd) [College]",
            Q138GRE == 97 ~ "12. Still study [Primary]",
            Q138GRE %in% c(98, 99) ~ NA_character_,
            #India
            Q138INDIA == 1 ~ "1. Illiterate [No education]",
            Q138INDIA == 2 ~ "2. Literate but no formal schooling [No education]",
            Q138INDIA == 3 ~ "3. Primary Education (primary certificate) [Primary]",
            Q138INDIA == 4 ~ "4. Upper Primary (Upper Primary Certificate) [Primary]",
            Q138INDIA == 5 ~ "5. High School; Industrial Training Institute (matriculation certificate or ITI certificate [Primary]",
            Q138INDIA == 6 ~ "6. Senior Secondary / intermediate (Senior Secondary School Leaving Certificate) [Primary]",
            Q138INDIA == 7 ~ "7. Technical Education Training; Junior teachers training (diploma) [Primary]",
            Q138INDIA == 8 ~ "8. Tertiary, technical higher education (diploma / bachelor) [Primary]",
            Q138INDIA == 9 ~ "9. Bachelor (university 1st) [College]",
            Q138INDIA == 10 ~ "10. Master (university  2nd) [College]",
            Q138INDIA == 11 ~ "11. Doctor (Tertiary 2nd) [College]",
            Q138INDIA == 97 ~ "12. STILL IN EDUCATION (Volunteered) [Primary]",
            Q138INDIA %in% c(98, 99) ~ NA_character_,
            #Indonesia
            Q138INDO == 1 ~ "1. No formal schooling [No education]",
            Q138INDO == 2 ~ "2. Incomplete elementary school [No education]",
            Q138INDO == 3 ~ "3. Complete elementary school (completed 6th grade) [Primary]",
            Q138INDO == 4 ~ "4. Incomplete junior high school (completed 1-2 secondary grade [Primary]",
            Q138INDO == 5 ~ "5. Complete junior high school (completed 9th grade) [Primary]",
            Q138INDO == 6 ~ "6. Incomplete high school (completed 1-2 high school grades) [Primary]",
            Q138INDO == 7 ~ "7. Completed senior high school (completed 12th grade) [Primary]",
            Q138INDO == 8 ~ "8. Diploma programmes (DI thru DIV) [Primary]", # given position on list
            Q138INDO == 9 ~ "9. Graduate diploma (SI) [College]", # given position on list
            Q138INDO == 10 ~ "10. Master degree [College]",
            Q138INDO == 11 ~ "11. Specialist II program [College]",
            Q138INDO == 97 ~ "12. Still in education [Primary]",
            Q138INDO %in% c(98, 99) ~ NA_character_,
            #Israel
            Q138ISR == 1 ~ "1. No formal schooling [No education]",
            Q138ISR == 2 ~ "2. Lowest formal qualification [Primary]",
            Q138ISR == 3 ~ "3. Vocational completed without Matriculation [Primary]",
            Q138ISR == 4 ~ "4. Yeshiva - NO matriculation certificate [Primary]",
            Q138ISR == 5 ~ "5. Lower secondary education - NO matriculation certificate [Primary]",
            Q138ISR == 6 ~ "6. Vocational completed with Matriculation [Primary]",
            Q138ISR == 7 ~ "7. Yeshiva - with matriculation certificate [Primary]",
            Q138ISR == 8 ~ "8. Upper secondary education, 3 or 4 years - matriculation certificate [Primary]",
            Q138ISR == 9 ~ "9. Preparatory programmes for practical engineers - matriculation certificate [Primary]",
            Q138ISR == 10 ~ "10. Pre-academic preparatory programmes - matriculation certificate [Primary]",
            Q138ISR == 11 ~ "11. Post-secondary (technician, engineer) teacher training (teacher diploma) [Primary]",
            Q138ISR == 12 ~ "12. Bachelor [College]",
            Q138ISR == 13 ~ "13. Master, Diploma [College]",
            Q138ISR == 14 ~ "14. Doctorate programme [College]",
            Q138ISR == 97 ~ "15. Still in education (Volunteered) [Primary]",
            Q138ISR %in% c(98, 99) ~ NA_character_,
            #Italy
            Q138ITA == 1 ~ "1. Senza titoli di studi/Without educational qualifications [No education]",
            Q138ITA == 2 ~ "2. Scuola primaria/Primary school [Primary]",
            Q138ITA == 3 ~ "3. Scuola secondaria di primo grado/Lower secondary school [Primary]",
            Q138ITA == 4 ~ "4. Scuola secondaria di secondo grado/Second grade secondary school [Primary]",
            Q138ITA == 5 ~ "5. Specializzazione professionale post-diploma/Post-diploma professional specialization [Primary]",
            Q138ITA == 6 ~ "6. Laurea triennale/Three-year degree [College]",
            Q138ITA == 7 ~ "7. Laurea magistrale/Master's degree [College]",
            Q138ITA == 8 ~ "8. Specializzazione post-laurea/Postgraduate specialization [College]",
            Q138ITA == 97 ~ "9. STO ANCORA STUDIANDO/Still studying [Primary]",
            Q138ITA %in% c(98, 99) ~ NA_character_,
            #Japan
            Q138JPN == 1 ~ "1. Did not receive any education / Kindergarten (Yochien) [No education]",
            Q138JPN == 2 ~ "2. Elementary school (Shogakko) [Primary]",
            Q138JPN == 3 ~ "3. Lower secundary school (Chuto-kyoiku-gakko (Zenki katei); Chugakko [Primary]",
            Q138JPN == 4 ~ "4. College of technology [Primary]",
            Q138JPN == 5 ~ "5. Upper Secondary school [Primary]",
            Q138JPN == 6 ~ "6. Junior college (associate degree), special training college (diploma = senmonshi or advanced diploma = kodo-senmonshi) [Primary]",
            Q138JPN == 7 ~ "7. Bachelor's degree (Gakushi) [College]",
            Q138JPN == 8 ~ "8. Master's degree (Shushi ) or Juris Doctor (Homuhakushi) [College]",
            Q138JPN == 9 ~ "9. Doctors degree (Hakushi) [College]",
            Q138JPN == 97 ~ "10. Still in education [Primary]",
            Q138JPN %in% c(98, 99) ~ NA_character_,
            #Jordan
            Q138JOR == 1 ~ "1. No formal education [No education]",
            Q138JOR == 2 ~ "2. Incomplete basic education [No education]",
            Q138JOR == 3 ~ "3. Basic education, 1st cycle (completed less than 9 grades) [Primary]",
            Q138JOR == 4 ~ "4. Basic education, 2nd cycle (completed 9 grades) [Primary]", 
            Q138JOR == 5 ~ "5. Applied secondary education (applied secondary certificate) [Primary]",
            Q138JOR == 6 ~ "6. Comprehensive secondary education (secondary education certificate) [Primary]",
            Q138JOR == 7 ~ "7. Community college (Community college diploma and success in the comprehensive exam) [Primary]",
            Q138JOR == 8 ~ "8. Bachelor [College]",
            Q138JOR == 9 ~ "9. Higher diploma programme (higher diploma) [College]",
            Q138JOR == 10 ~ "10. Masters [College]",
            Q138JOR == 11 ~ "11. Doctorate [College]",
            Q138JOR == 97 ~ "12. Still in School [Primary]",
            Q138JOR %in% c(98, 99) ~ NA_character_,
            #Kenya
            Q138KEN == 1 ~ "1. No formal education [No education]",
            Q138KEN == 2 ~ "2. Early childhood development programme (ECD) [No education]",
            Q138KEN == 3 ~ "3. First stage of primary education (Standards 1 to 6) [Primary]",
            Q138KEN == 4 ~ "4. Second stage of primary education (Standards 7 to 8) (KCPE) [Primary]",
            Q138KEN == 5 ~ "5. Youth polytechnics programme (Craft trade certificate) [Primary]",
            Q138KEN == 6 ~ "6. Youth polytechnics programme (Technical vocational certificate) [Primary]",
            Q138KEN == 7 ~ "7. Incomplete secondary school [Primary]",
            Q138KEN == 8 ~ "8. Secondary education (KCSE) [Primary]",
            Q138KEN == 9 ~ "9. High School (Form 6) A level Certificate [Primary]",
            Q138KEN == 10 ~ "10. Technical and industrial vocational education training (TIVET) [Primary]",
            Q138KEN == 11 ~ "11. Teacher training; Polytechnics programmes [Primary]",
            Q138KEN == 12 ~ "12. Bachelor [College]",
            Q138KEN == 13 ~ "13. Masters [College]",
            Q138KEN == 14 ~ "14. Doctorate [College]",
            Q138KEN == 97 ~ "15. Still in education [Primary]",
            Q138KEN %in% c(98, 99) ~ NA_character_,
            #Lebanon
            Q138LEB == 1 ~ "1. Préprimaire or didn't finish Primaire/Primary or didn't finish primary [No education]",
            Q138LEB == 2 ~ "2. Primaire (1 e et 2 e cycle, enseignement principale)/Primary (1st and 2nd cycle primary education) [Primary]",
            Q138LEB == 3 ~ "3. Certificat d'aptitude professionnelle or Brevet professionel (C.A.P.;B.P.)/Professional certificate or Certificate Professional (CAP, BP) [Primary]",
            Q138LEB == 4 ~ "4. Moyen, 3 e cycle (BREVET, enseignement principale)/Moyen, 3rd cycle (BREVET, Senior Education) [Primary]",
            Q138LEB == 5 ~ "5. Secondaire professionnel (Baccalauréat général (S.G.))/Professional secondary (general Baccalaureate (SG)) [Primary]",
            Q138LEB == 6 ~ "6. Baccalauréat technique (Baccalauréat technique (B.T.))/Bachelor of Technology (Bachelor of Technology (BT)) [Primary]", # unclear but these appear to be higher edu degrees
            Q138LEB == 7 ~ "7. Secondaire (Baccalauréat général (S.G.))/Secondary (general Baccalaureate (S. G.)) [Primary]",
            Q138LEB == 8 ~ "8. Technique supérieur (T.S.); Diplôme d'études spécialisées (D.E.S.); Licence d'enseignement technique (L.E.T.)/Superior Técnical (T.S.); Specialized diploma (D.E.S.); Technical education license (L.E.T.) [Primary]",
            Q138LEB == 9 ~ "9. Maîtrise; Licence/1st Master, License [College]",
            Q138LEB == 10 ~ "10. Majistère;  Diplôme d'études approfondies (D.E.A.); Doctorat en médecine/2nd Master; Postgraduate Diploma (D.E.A.); MD [College]",
            Q138LEB == 11 ~ "11. Doctorat/Doctorate [College]",
            Q138LEB == 97 ~ "12. Still in education [Primary]",
            Q138LEB %in% c(98, 99) ~ NA_character_,
            #Malaysia
            Q138MAL == 1 ~ "1. No formal education [No education]",
            Q138MAL == 2 ~ "2. Primary school (Standard 1 to Standard 6 / UPSR) [Primary]",
            Q138MAL == 3 ~ "3. Lower secondary school (Form 1 to Form 3 / PMR / SRP / LCE) [Primary]",
            Q138MAL == 4 ~ "4. Vocational / technical (vocational / technical school, e.g. SPVM / MCVE) [Primary]",
            Q138MAL == 5 ~ "5. Upper secondary school (Form 4 to Form 5 / SPM / MCE / SC / GCE ‘O’ Level) [Primary]",
            Q138MAL == 6 ~ "6. Trade and technical skills institution  (Studies in trade and technical skills institution, e.g. certificate from SKM) [Primary]",
            Q138MAL == 7 ~ "7.  Post Secondary (Post-secondary studies, e.g. STPM / STP / STA / 4THANAWI / HSC / GCE ‘A’ Level, diploma) [Primary]",
            Q138MAL == 8 ~ "8. Tertiary (Tertiary studies, e.g. post-diploma / college / polytechnic / university education) [College]",
            Q138MAL == 9 ~ "9. Post graduate Master (Sarjana) [College]",
            Q138MAL == 10 ~ "10. Post graduate (PhD) [College]",
            Q138MAL == 97 ~ "11. Still in education [Primary]",
            Q138MAL %in% c(98, 99) ~ NA_character_,
            #Mexico
            Q138MEX == 1 ~ "1. Sin educación formal [No education]",
            Q138MEX == 2 ~ "2. Escuela primaria incompleta [No education]",
            Q138MEX == 3 ~ "3. Escuela primaria completa [Primary]",
            Q138MEX == 4 ~ "4. Capacitación para el trabajo [Primary]",
            Q138MEX == 5 ~ "5. Educación secundaria incompleta [Primary]",
            Q138MEX == 6 ~ "6. Educación secundaria [Primary]",
            Q138MEX == 7 ~ "7. Educación técnica posterior a Secundaria [Primary]",
            Q138MEX == 8 ~ "8. Bachillerato tecnológico [Primary]",
            Q138MEX == 9 ~ "9. Bachillerato general [Primary]",
            Q138MEX == 10 ~ "10. Educación técnica posterior a Bachillerato [Primary]",
            Q138MEX == 11 ~ "11. Algunos estudios universitarios, incompletos [Primary]",
            Q138MEX == 12 ~ "12. Licenciatura universitaria, Educación normal, Licenciatura [College]",
            Q138MEX == 13 ~ "13. Maestría [College]",
            Q138MEX == 14 ~ "14. Doctorado [College]",
            Q138MEX == 97 ~ "15. Still in Education [Primary]",
            Q138MEX %in% c(98, 99) ~ NA_character_,
            #Nicaragua
            Q138NIC == 1 ~ "1. Sin estudios, Educacion Parvularia/Without studies, Early Childhood Education [No education]",
            Q138NIC == 2 ~ "2. Sin estudios, pero sabe leer y escribir/No education, but knows how to read and write [No education]",
            Q138NIC == 3 ~ "3. Primaria incompleta/Primary incomplete [No education]",
            Q138NIC == 4 ~ "4. Primaria complete/Primary complete [Primary]",
            Q138NIC == 5 ~ "5. Secundaria o bachillerato incompleta/Incomplete high school [Primary]",
            Q138NIC == 6 ~ "6. Secundaria o bachillerato/Complete high school [Primary]",
            Q138NIC == 7 ~ "7. Técnica/Tecnológica incompleta/Incomplete technical/technological [Primary]",
            Q138NIC == 8 ~ "8. Técnica/Tecnológica complete/Complete technical/technological [Primary]",
            Q138NIC == 9 ~ "9. Educacion Superior Universitario incompleta/Incomplete University Higher Education [Primary]",
            Q138NIC == 10 ~ "10. Educacion Superior Universitario (Bachelor) [College]",
            Q138NIC == 11 ~ "11. Educacion Superior Universitario (Master) [College]",
            Q138NIC == 12 ~ "12. Postgrado Universitario // Doctorado/University post-graduate/Doctorate [College]",
            Q138NIC == 97 ~ "13. Still in education [Primary]",
            Q138NIC %in% c(98, 99) ~ NA_character_,
            #Nigeria
            Q138NIG == 1 ~ "1. Pre-primary education [No education]",
            Q138NIG == 2 ~ "2. Primary education [Primary]",
            Q138NIG == 3 ~ "3. Vocational enterprise institutions programmes [Primary]",
            Q138NIG == 4 ~ "4. Junior secondary [Primary]",
            Q138NIG == 5 ~ "5. Secondary technical schools programmes [Primary]",
            Q138NIG == 6 ~ "6. Senior secondary [Primary]",
            Q138NIG == 7 ~ "7. Certificate courses [Primary]",
            Q138NIG == 8 ~ "8. Interim joint matriculation board (IJMB) A- level course [Primary]",
            Q138NIG == 9 ~ "9.  National Diploma, NCE [Primary]",
            Q138NIG == 10 ~ "10. Higher National Diploma [Primary]",
            Q138NIG == 11 ~ "11. Bachelor [College]",
            Q138NIG == 12 ~ "12. Master [College]",
            Q138NIG == 13 ~ "13. PhD [College]",
            Q138NIG == 97 ~ "14. Still in education [Primary]",
            Q138NIG %in% c(98, 99) ~ NA_character_,
            #Pakistan
            Q138PAK == 1 ~ "1. No education at all / Illiterate [No education]",
            Q138PAK == 2 ~ "2. No formal education but can read/write simple Urdu [No education]",
            Q138PAK == 3 ~ "3. Incomplete primary education (completed less than 5 grades) [No education]",
            Q138PAK == 4 ~ "4. Complete primary education (completed 5 grades) [Primary]",
            Q138PAK == 5 ~ "5. Vocational institutes (Certificate, Diploma) [Primary]", 
            Q138PAK == 6 ~ "6. Incomplete middle school (completed 6 or 7 grades) [Primary]",
            Q138PAK == 7 ~ "7. Complete middle school (completed 8 grades) [Primary]",
            Q138PAK == 8 ~ "8. Primary teaching certificate (PTC) [Primary]",
            Q138PAK == 9 ~ "9. Technical education; Nursing (Certificate and Diploma; RN) [Primary]",
            Q138PAK == 10 ~ "10. High level (IX-X) [Primary]",
            Q138PAK == 11 ~ "11. Higher secondary or Intermediate (Grades XI - XII) (Secondary school certificate or Matriculation) [Primary]",
            Q138PAK == 12 ~ "12. Certificate in training (CT) or Paramedical [Primary]",
            Q138PAK == 13 ~ "13. Bachelor [College]",
            Q138PAK == 14 ~ "14. Master, Medical degree [College]",
            Q138PAK == 15 ~ "15. Doctorate [College]",
            Q138PAK %in% c(98, 99) ~ NA_character_,
            #Palestine
            Q138PAL == 1 ~ "1. No formal education / Kindergarten [No education]",
            Q138PAL == 2 ~ "2. Incomplete elementary education (completed less than 5 grades under new system or less than 6 grades under old system) [No education]",
            Q138PAL == 3 ~ "3. Complete elementary education (completed 5 grades under new system or 6 grades under old system) [Primary]",
            Q138PAL == 4 ~ "4. Complete preparatory education (completed 10 grades under new system or 9 grades under old system) [Primary]",
            Q138PAL == 5 ~ "5. Not completed secondary education (completed 11 grades) [Primary]",
            Q138PAL == 6 ~ "6. Secondary Vocational (completed 12 grades) [Primary]",
            Q138PAL == 7 ~ "7. Complete secondary education (completed 12 grades) [Primary]",
            Q138PAL == 8 ~ "8. Diploma // Vocational training [Primary]",
            Q138PAL == 9 ~ "9. Bachelor [College]",
            Q138PAL == 10 ~ "10. Master [College]",
            Q138PAL == 11 ~ "11. PhD [College]",
            Q138PAL == 97 ~ "12. Still in Education (Volunteered) [Primary]",
            Q138PAL %in% c(98, 99) ~ NA_character_,
            #Peru
            Q138PER == 1 ~ "1. No education / Kindergarten (Preschool 3 to 5 years old) [No education]",
            Q138PER == 2 ~ "2. Primary education incomplete [No education]",
            Q138PER == 3 ~ "3. Primary education complete [Primary]",
            Q138PER == 4 ~ "4. Secondary education incomplete [Primary]",
            Q138PER == 5 ~ "5. Secondary education complete [Primary]",
            Q138PER == 6 ~ "6. University incomplete [Primary]",
            Q138PER == 7 ~ "7. Not university pedagogy and art education , Military Academy [Primary]",
            Q138PER == 8 ~ "8. Technical Education , Military Academy  complete [College]", # assumed given position
            Q138PER == 9 ~ "9. Master’s degree [College]",
            Q138PER == 10 ~ "10. PhD’s degree [College]",
            Q138PER == 97 ~ "11. Still studying (Volunteered) [Primary]",
            Q138PER %in% c(98, 99) ~ NA_character_,
            #Philippines
            Q138PHI == 1 ~ "1. No formal education [No education]",
            Q138PHI == 2 ~ "2. Some elementary [No education]",
            Q138PHI == 3 ~ "3. Finished elementary [Primary]",
            Q138PHI == 4 ~ "4. Finished high school (Secondary, General (years 1-3) [Primary]",
            Q138PHI == 5 ~ "5. Finished college (Secondary, general (year 4)) [Primary]",
            Q138PHI == 6 ~ "6. Finished a vocational (Certificate of Proficiency) [Primary]",
            Q138PHI == 7 ~ "7. Generally done ( Certificate of Proficiency) [Primary]",
            Q138PHI == 8 ~ "8. Bachelor [College]",
            Q138PHI == 9 ~ "9. Master s degree: Bachelor s degree in medicine, dentistry, engineering, law [College]",
            Q138PHI == 10 ~ "10. Doctorate Degree [College]",
            Q138PHI == 97 ~ "11. STILL IN EDUCATION [Primary]",
            Q138PHI %in% c(98, 99) ~ NA_character_,
            #Poland
            Q138POL == 1 ~ "1. Brak formalnego wykszta?cenia lub nieuko?czone podstawowe/No formal education or not completed basic [No education]",
            Q138POL == 2 ~ "2. Podstawowe – 6 klas, lub nieuko?czone gimnazjum/Elementary - 6 classes, or incomplete junior high school [Primary]",
            Q138POL == 3 ~ "3. Gimnazjum lub podstawowe 8 klas/Junior high school or elementary 8 classes [Primary]",
            Q138POL == 4 ~ "4. Nieko?czona szko?a zawodowa/Incomplete vocational school [Primary]",
            Q138POL == 5 ~ "5. Szko?a zawodowa (2-3 lata) LUB nie uko?czona szko?a ?rednia/Vocational school (2-3 years) OR not Completed high school [Primary]",
            Q138POL == 6 ~ "6. Uko?czona szko?a ?rednia (liceum ogólnokszta?c?ce lub technikum)/Completed high school (high school or technical high school) [Primary]",
            Q138POL == 7 ~ "7. Nieuko?czone studia licencjackie/ nieuko?czona szko?a pomaturalna/Unfinished bachelor's degree / unfinished College [Primary]",
            Q138POL == 8 ~ "8. Uko?czona szko?a pomaturalne/Graduated College [Primary]",
            Q138POL == 9 ~ "9. Uko?czone studia licencjackie/in?ynierskie/Completed bachelor / engineering studies [College]",
            Q138POL == 10 ~ "10. Nieuko?czone studia wy?sze (magisterskie)/Unfinished higher education (master's) [College]",
            Q138POL == 11 ~ "11. Uko?czone studia wy?sze (magisterskie)/Completed higher education (master's) [College]",
            Q138POL == 12 ~ "12.  Studia doktoranckie, doktor habilitowany, tytu? profesora/PhD studies, habilitated doctor, titled professor [College]",
            Q138POL == 97 ~ "13. Still a student [Primary]",
            Q138POL %in% c(98, 99) ~ NA_character_,
            #Russia
            Q138RUS == 1 ~ "1. Never attended school or only completed grade school 1-2 (unfinished elementary school) [No education]",
            Q138RUS == 2 ~ "2. general primary or incomplete lower secondary [Primary]",
            Q138RUS == 3 ~ "3. graduated from 3-7 years of high school , but has not received a certificate of basic general education [Primary]",
            Q138RUS == 4 ~ "4. Complete general secondary education (10 years under the old system, 11 years for a new one), received a certificate [Primary]",
            Q138RUS == 5 ~ "5. Initial vocational education - graduated from vocational schools, trade schools, factory training, vocational Lyceum [Primary]",
            Q138RUS == 6 ~ "6. Initial vocational education - graduated from vocational school, vocational school, who also gave general secondary edu [Primary]",
            Q138RUS == 7 ~ "7. Vocational education - graduated from technical school , college (2-4 years) [Primary]",
            Q138RUS == 8 ~ "8. received a bachelors degree in high school after four years of training on the new two-tier system [Primary]", #unsure, ambiguity here
            Q138RUS == 9 ~ "9. Higher education for 5 -6- year system (diploma) [College]",
            Q138RUS == 10 ~ "10. Academic degree (PhD, PhD) [College]",
            Q138RUS == 97 ~ "11. Still in school [Primary]",
            Q138RUS %in% c(98, 99) ~ NA_character_,
            #Senegal
            Q138SEN == 96 ~ "0. No formal education (no schooling) [No education]",
            Q138SEN == 1 ~ "1. Pre-primary education [No education]",
            Q138SEN == 2 ~ "2. Primary education (CFEE) [Primary]",
            Q138SEN == 3 ~ "3. Vocational education (CAP) [Primary]",
            Q138SEN == 4 ~ "4. Lower secondary education/ (BFEM) [Primary]",
            Q138SEN == 5 ~ "5. Lower secondary vocational education (BEP) [Primary]",
            Q138SEN == 6 ~ "6. Teacher training  (CEAP); Upper secondary vocational education (technical baccalaureate) [Primary]",
            Q138SEN == 7 ~ "7. Upper secondary general education  (baccalaureate) [Primary]",
            Q138SEN == 8 ~ "8. Post-secondary non-tertiary general education / Teacher training (CACEM); (CAP) [Primary]",
            Q138SEN == 9 ~ "9. Post-secondary non-tertiary vocational education / Teacher training (CAEM, DUT, BTS); Higher Vocational Education (CAES) [Primary]",
            Q138SEN == 10 ~ "10. Post-secondary non-tertiary education (Bachelor’s or equivalent level) [College]",
            Q138SEN == 11 ~ "11. Post-secondary non-tertiary education (Master’s or equivalent level) [College]",
            Q138SEN == 12 ~ "12. Post-secondary non-tertiary education (Doctorate, Master 2, DEA) [College]",
            Q138SEN == 97 ~ "13. Still in Education [Primary]",
            Q138SEN %in% c(98, 99) ~ NA_character_,
            #South Russia
            Q138RSA == 1 ~ "1. No formal education [No education]",
            Q138RSA == 2 ~ "2. Incomplete primary school [No education]",
            Q138RSA == 3 ~ "3. Complete primary school [Primary]",
            Q138RSA == 4 ~ "4. National certificate (vocational NC(V)2 - 4); Nated courses level 1-3 (N1- N3) / Other post-primary vocational programme [Primary]",
            Q138RSA == 5 ~ "5. Some high school [Primary]",
            Q138RSA == 6 ~ "6. Matric/Grade 12 [Primary]",
            Q138RSA == 7 ~ "7. Artisans certificate obtained [Primary]",
            Q138RSA == 8 ~ "8. Technikon diploma/degree completed [Primary]",
            Q138RSA == 9 ~ "9. University degree completed [College]",
            Q138RSA == 10 ~ "10. Professional [Primary]",
            Q138RSA == 11 ~ "11. Technical [Primary]",
            Q138RSA == 12 ~ "12. Secretarial [Primary]",
            Q138RSA == 13 ~ "13. Other (SPECIFY) [Primary]",
            Q138RSA == 97 ~ "14. Still in education [Primary]",
            Q138RSA %in% c(98, 99) ~ NA_character_,
            #South Korea
            Q138SKOR == 1 ~ "1. No formal schooling / Yuchiwon (kindergarten) [No education]",
            Q138SKOR == 2 ~ "2. Incomplete primary education [No education]",
            Q138SKOR == 3 ~ "3. Chodeung-hakgyo (primary school) [Primary]",
            Q138SKOR == 4 ~ "4. Dropped out of middle school [Primary]",
            Q138SKOR == 5 ~ "5. Jung-hakgyo (middle school) [Primary]",
            Q138SKOR == 6 ~ "6. Jeonmungye kodeung-hakgyo (vocational highshool) / Kodeung kisul-hakgyo (trade high school) [Primary]",
            Q138SKOR == 7 ~ "7. Ilbangye kodeung-hakgyo (general high school) [Primary]",
            Q138SKOR == 8 ~ "8. Daehak (junior college, polytechnic college // associate's degree) Semu daehak (college of taxation) or Yukkun samsakwan [Primary]",
            Q138SKOR == 9 ~ "9. Bachelor's degree [College]",
            Q138SKOR == 10 ~ "10. Master's degree [College]",
            Q138SKOR == 11 ~ "11. Doctor's degree [College]",
            Q138SKOR == 97 ~ "12. STILL IN EDUCATION [Primary]",
            Q138SKOR %in% c(98, 99) ~ NA_character_,
            #Spain
            Q138SPA == 16 ~ "0. Sin estudios. No sabe leer ni escribir (analfabeto)/No studies. Cannot read or write (illiterate) [No education]",
            Q138SPA == 1 ~ "1. Menos de 5 años de escolarización/Educación Infantil/Less than 5 years of schooling/Early Childhood Education [No education]",
            Q138SPA == 2 ~ "2. Estudios primarios sin completar/Uncompleted primary studies [No education]",
            Q138SPA == 3 ~ "3. Certificado de Estudios Primarios/Primer Grado (Certificado escolar EGB 1 etapa mas o menos 10 años)/Certificate of Primary Studies / First Grade (EGB school certificate 1 stage more or less 10 years) [Primary]",
            Q138SPA == 4 ~ "4. Transición a la vida adulta/Transition to adult life [Primary]",
            Q138SPA == 5 ~ "5. E.E. Elementales de Danza y Música (certificado elemental)/E.E. Elementals of Dance and Music (elementary certificate) [Primary]",
            Q138SPA == 6 ~ "6. Segundo grado primer ciclo (Graduado escolar o EGB 2 etapa o 1º o 2º ESO primer ciclo hasta 14 años)/Second grade first cycle (School graduate or EGB 2 stage or 1st or 2nd ESO first cycle up to 14 years old) [Primary]",
            Q138SPA == 7 ~ "7. Formación Profesional de grado medio (certification)/Intermediate vocational training (certification) [Primary]",
            Q138SPA == 8 ~ "8. Ciclos Formativos  de Grado Medio (Técnico)/Intermediate Training Cycles (Technical) [Primary]",
            Q138SPA == 9 ~ "9. Segundo grado segundo ciclo (FP I y II Bachiller superior BUP 3º y 4º ESO segundo ciclo COU PREU 1º y 2º Bachillerato ha/Second grade second cycle (FP I and II Upper Baccalaureate BUP 3rd and 4th ESO second cycle COU PREU 1st and 2nd Baccalaureate has [Primary]",
            Q138SPA == 10 ~ "10. C.F. de Grado Superior (Técnico Superior)/C.F. of Higher Degree (Higher Technician) [Primary]",
            Q138SPA == 11 ~ "11. Pruebas de acceso a Ciclos Formativos de Grado Superior, Graduado en Artes Aplicadas u Oficios Artisticos/Tests for access to Higher Degree Training Courses, Graduate in Applied Arts or Artistic Trades [Primary]",
            Q138SPA == 12 ~ "12. Formacion Professional de 2º Grado/2º Degree Vocational Training [Primary]",
            Q138SPA == 13 ~ "13. Grado/Degree [College]",
            Q138SPA == 14 ~ "14. Master oficial (Master degree) [College]",
            Q138SPA == 15 ~ "15. Titulaciones oficiales de estudios de postgrado (Doctorado, specialidades Médicas)/Official qualifications of postgraduate studies (Doctorate, Medical specialties) [College]",
            Q138SPA == 97 ~ "17. Still a student [Primary]",
            Q138SPA %in% c(98, 99) ~ NA_character_,
            #Tanzania
            Q138TAN == 1 ~ "1. No formal education [No education]",
            Q138TAN == 2 ~ "2. Some Primary school [No education]",
            Q138TAN == 3 ~ "3. Completed primary school (Primary School Leaving Certificate - PSLC) [Primary]",
            Q138TAN == 4 ~ "4. Teacher training (CSE) / Vocational training (Grade B Certificate) [Primary]",
            Q138TAN == 5 ~ "5. Incomplete secondary education [Primary]",
            Q138TAN == 6 ~ "6. Lower secondary education [Primary]",
            Q138TAN == 7 ~ "7. Teacher training, Nursing, Secretarial courses (Diploma / Certificate) [Primary]",
            Q138TAN == 8 ~ "8. Upper secondary (A-Level: Advanced Certificate of Secondary Education (ACSE)) [Primary]",
            Q138TAN == 9 ~ "9. A-Level Advanced Certificate of Secondary Education (ASCSE) (Professional Advanced Training / Post Graduate Education) [Primary]",
            Q138TAN == 10 ~ "10. Bachelor [College]",
            Q138TAN == 11 ~ "11. Masters [College]",
            Q138TAN == 12 ~ "12. PhD [College]",
            Q138TAN == 97 ~ "13. Still in school [No education]",
            Q138TAN %in% c(98, 99) ~ NA_character_,
            #Thailand
            Q138THA == 1 ~ "1. No formal schooling (Preschool or Kindergarten) [No education]",
            Q138THA == 2 ~ "2. Incomplete primary [No education]",
            Q138THA == 3 ~ "3. Primary [Primary]",
            Q138THA == 4 ~ "4. Lower secondary education (Lower secondary education certificate) [Primary]",
            Q138THA == 5 ~ "5. Upper secondary, vocational (Vocational education certificate) [Primary]",
            Q138THA == 6 ~ "6. Upper secondary, general (Upper secondary education certificate) [Primary]",
            Q138THA == 7 ~ "7. Post-secondary non-tertiary (Post-secondary certificate) [Primary]",
            Q138THA == 8 ~ "8. Diploma in vocational education [Primary]",
            Q138THA == 9 ~ "9. Bachelor's degree [College]",
            Q138THA == 10 ~ "10. Master's degree, Graduate diploma [College]",
            Q138THA == 11 ~ "11. Doctor's degree (Ph.D.) [College]",
            Q138THA %in% c(98, 99) ~ NA_character_,
            #Tunisia
            Q138TUN == 1 ~ "1. Pré-primaire,Classe préparatoire/Pre-primary,Preparatory class [No education]",
            Q138TUN == 2 ~ "2. 1 e cycle de l'enseignement de base/1st cycle of basic education [Primary]",
            Q138TUN == 3 ~ "3. Programme d'apprentissage/Apprenticeship program [Primary]",
            Q138TUN == 4 ~ "4. Enseignement préparatoire technique/Technical preparatory education [Primary]",
            Q138TUN == 5 ~ "5. 2 e cycle de l'enseignement de base/2nd cycle of basic education [Primary]",
            Q138TUN == 6 ~ "6. Formation professionnelle (CAP)/Vocational training (CAP) [Primary]",
            Q138TUN == 7 ~ "7. Formation professionnelle (BTP)/Vocational training (BTP) [Primary]",
            Q138TUN == 8 ~ "8. Enseignement secondaire général/General secondary education [Primary]",
            Q138TUN == 9 ~ "9. Baccalauréat ou BTP,licence appliquée,iset/Baccalaureate or BTP, applied license, iset [Primary]",
            Q138TUN == 10 ~ "10. Baccalauréat/Baccalaureate [Primary]", # is this high school completion?
            Q138TUN == 11 ~ "11. Licence fondamentale/Fundamental licence [College]",
            Q138TUN == 12 ~ "12. Doctorat/Doctorate [College]",
            Q138TUN == 97 ~ "13. Still in education [Primary]",
            Q138TUN %in% c(98, 99) ~ NA_character_,
            #Turkey
            Q138TUR == 1 ~ "1. No formal education [No education]",
            Q138TUR == 2 ~ "2. Incomplete primary school (completed less than 5 grades) [No education]",
            Q138TUR == 3 ~ "3. Complete primary school (completed 5 grades) [Primary]",
            Q138TUR == 4 ~ "4. Complete primary education (completed 8 grades) [Primary]",
            Q138TUR == 5 ~ "5. Complete junior high school [Primary]",
            Q138TUR == 6 ~ "6. Complete vocational school at junior high school level [Primary]",
            Q138TUR == 7 ~ "7. Complete high school [Primary]",
            Q138TUR == 8 ~ "8. Complete vocational school at high school level [Primary]",
            Q138TUR == 9 ~ "9. Higher education (completed 2 years of college or 4 years of university studies) [College]",
            Q138TUR == 10 ~ "10. Master (completed 2 more years after university) [College]",
            Q138TUR == 11 ~ "11. PhD (completed 4-6 more years after university) [College]",
            Q138TUR %in% c(98, 99) ~ NA_character_,
            #Uganda
            Q138UGA == 1 ~ "1. No education at all [No education]",
            Q138UGA == 2 ~ "2. Early childhood education [No education]",
            Q138UGA == 3 ~ "3. Some primary (P1-P6) [No education]",
            Q138UGA == 4 ~ "4. Complete primary (Completed P7 / PLE) [Primary]",
            Q138UGA == 5 ~ "5. Post-primary, non-lower secondary [Primary]",
            Q138UGA == 6 ~ "6. Some secondary (S1-S5) / Lower secondary - O' level [Primary]",
            Q138UGA == 7 ~ "7. Upper secondary - other [Primary]",
            Q138UGA == 8 ~ "8. Completed secondary / Upper secondary - A' level [Primary]",
            Q138UGA == 9 ~ "9. College (Diploma) [Primary]",
            Q138UGA == 10 ~ "10. Tertiary / Health college (Diploma in Health) [Primary]",
            Q138UGA == 11 ~ "11. Tertiary or post-graduate education (Bachelor, Diploma) [College]",
            Q138UGA == 12 ~ "12. Tertiary education (Master) [College]",
            Q138UGA == 13 ~ "13. Post University ( Doctoral studies / Doctor) [College]",
            Q138UGA == 97 ~ "14. Still studying [Primary]",
            Q138UGA %in% c(98, 99) ~ NA_character_,
            #Ukraine
            Q138UKR == 1 ~ "1. Incomplete primary education [No education]",
            Q138UKR == 2 ~ "2. Primary education (4-7 grades of secondary school) [Primary]",
            Q138UKR == 3 ~ "3. Incomplete secondary education [Primary]",
            Q138UKR == 4 ~ "4. PTU on the base of incomplete secondary education [Primary]",
            Q138UKR == 5 ~ "5. Complete secondary education [Primary]",
            Q138UKR == 6 ~ "6. Completed PTU on the base of incomplete secondary education [Primary]",
            Q138UKR == 7 ~ "7. Additional education on the base of full secondary education [Primary]",
            Q138UKR == 8 ~ "8. PTU on the base of full secondary education [Primary]",
            Q138UKR == 9 ~ "9. Incomplete high education [Primary]",
            Q138UKR == 10 ~ "10. Basic high education (bachelor) [College]",
            Q138UKR == 11 ~ "11. Complete high education (specialist) [College]",
            Q138UKR == 12 ~ "12. Full high education (master) [College]",
            Q138UKR == 13 ~ "13. Aspirantura, scientific degree [College]",
            Q138UKR == 97 ~ "14.  Still studying [Primary]",
            Q138UKR %in% c(98, 99) ~ NA_character_,
            #US
            Q138US == 1 ~ "1. No formal schooling (Preschool or Kindergarten) [No education]",
            Q138US == 2 ~ "2. Less than high school (grades 1 thru 8) [Primary]",
            Q138US == 3 ~ "3. High school incomplete (Grades 9-11 or Grade 12 with NO diploma) [Primary]",
            Q138US == 4 ~ "4. High school graduate (GED or High school diploma) [Primary]",
            Q138US == 5 ~ "5. Vocational Certificate (Occupationally specific vocational certificate) [Primary]",
            Q138US == 6 ~ "6. Vocational Associate's Degree Program [Primary]",
            Q138US == 7 ~ "7. Some college, no degree (includes community college) [Primary]",
            Q138US == 8 ~ "8. Two year associate degree from a college or university [Primary]",
            Q138US == 9 ~ "9. Four year college or university degree/Bachelor’s degree (e.g., BS, BA, AB) [College]",
            Q138US == 10 ~ "10. Postgraduate or professional degree, including master’s degree (e.g., MA, MS, PhD, MD, JD, graduate school) [College]",
            Q138US == 97 ~ "11. STILL IN EDUCATION [Primary]",
            Q138US %in% c(98, 99) ~ NA_character_,
            #Venezuela
            Q138VEN == 1 ~ "1. Without formal education [No education]",
            Q138VEN == 2 ~ "2. Elementary education incomplete [No education]",
            Q138VEN == 3 ~ "3. Elementary education complete [Primary]",
            Q138VEN == 4 ~ "4. Secondary education incomplete [Primary]",
            Q138VEN == 5 ~ "5. Secondary education complete [Primary]",
            Q138VEN == 6 ~ "6. Highschool incomplete [Primary]",
            Q138VEN == 7 ~ "7. Highschool complete [Primary]",
            Q138VEN == 8 ~ "8. Associate degree incomplete [Primary]",
            Q138VEN == 9 ~ "9. Associate degree complete [Primary]",
            Q138VEN == 10 ~ "10. Bachellor degree incomplete [Primary]",
            Q138VEN == 11 ~ "11. Bachellor degree complete [College]",
            Q138VEN == 12 ~ "12. Master/Doctorate incomplete [College]",
            Q138VEN == 13 ~ "13. Master Complete [College]",
            Q138VEN == 14 ~ "14. Doctorate complete [College]",
            Q138VEN == 97 ~ "15. Still in education [Primary]",
            Q138VEN %in% c(98, 99) ~ NA_character_,
            #Vietnam
            Q138VIE == 1 ~ "1. Pre-primary [No education]",
            Q138VIE == 2 ~ "2. Incomplete primary [No education]",
            Q138VIE == 3 ~ "3. Primary [Primary]",
            Q138VIE == 4 ~ "4. Lower secondary education (Lower secondary education certificate) [Primary]",
            Q138VIE == 5 ~ "5. Upper secondary, vocational (Vocational education certificate) [Primary]",
            Q138VIE == 6 ~ "6. Upper secondary, general (Upper secondary education certificate) [Primary]",
            Q138VIE == 7 ~ "7. Post-secondary non-tertiary (Post-secondary certificate) [Primary]",
            Q138VIE == 8 ~ "8. Diploma in vocational education [Primary]",
            Q138VIE == 9 ~ "9. Bachelors degree [College]",
            Q138VIE == 10 ~ "10. Masters degree, Graduate diploma [College]",
            Q138VIE == 11 ~ "11. Doctors degree (Ph.D.) [College]",
            Q138VIE == 97 ~ "12. STILL IN EDUCATION [Primary]",
            Q138VIE %in% c(98, 99) ~ NA_character_),       
      
      # respondent's gender (numeric: female = 1; male = 0; other = NA)
        resp_female =
          case_when(
            Q132 == 2 ~ 1,
            Q132 == 1 ~ 0),
      
      # respondent resident in rural (vs urban) area (numeric: rural = 1; urban/semi-urban/peri-urban = 0)
        resp_rural = 
          case_when(
            Q178FRA %in% c(2:5)|
            Q179GER %in% c(1:2)| # did not use Q178GER (the two are not consistent)
            Q178NIC == 1|
            Q178PHI == 1|
            Q178RSA %in% c(1:2)|
            Q178SKOR %in% c(1:2)|
            Q178SPA %in% c(1:2)|
            Q179CHL == 1|
            Q179EGY == 1|
            Q179ESA == 1|
            Q179ISR == 1|
            Q179JPN == 1|
            Q179LEB == 1|
            Q179TAN == 1|
            Q179THAI == 1|
            Q179VEN == 1|
            Q180GHA == 1|
            Q180GRE == 1| # did not use Q181 cuz it's consistent with Q180GRE
            Q180ITA == 1|
            Q180JOR == 1|
            Q180NIG %in% c(1:2)|
            Q180POL == 1|
            Q180TUN == 1|
            Q180VIE == 1|
            Q181BRA == 1|
            Q181COL == 1|
            Q181INDO == 1|
            Q181KEN == 1|
            Q181MAL == 1|
            Q181MEX %in% c(1, 3)|
            Q181PAL %in% c(1, 3)| # refugee camps as urban
            Q181PER %in% c(1:2)|
            Q181TUR == 1|
            Q181UKR == 1|
            Q182ARG == 1|
            Q182BAN == 1|
            Q182CHI %in% c(1:2)|
            Q182PAK == 1|
            Q182RUS %in% c(1:4)|
            Q182UGA == 1|
            Q185INDIA == 1|
            USR %in% c("R", "S") ~ 0,
            
            Q178FRA %in% c(1)|
            Q179GER == 3|
            Q178NIC == 2|
            Q178PHI == 2|
            Q178RSA == 3|
            Q178SKOR == 3|
            Q178SPA == 3|
            Q179CHL == 2|
            Q179EGY == 2|
            Q179ESA == 2|
            Q179ISR == 2|
            Q179JPN == 2|
            Q179LEB == 2|
            Q179TAN == 2|
            Q179THAI == 2|
            Q179VEN == 2|
            Q180GHA == 2|
            Q180GRE == 2|
            Q180ITA == 2|
            Q180JOR == 2|
            Q180NIG == 3|
            Q180POL == 2|
            Q180TUN == 2|
            Q180VIE == 2|
            Q181BRA == 2|
            Q181COL == 2|
            Q181INDO == 2|
            Q181KEN == 2|
            Q181MAL == 2|
            Q181MEX == 2|
            Q181PAL == 2|
            Q181PER == 3|
            Q181TUR == 2|
            Q181UKR == 2|
            Q182ARG == 2|
            Q182BAN == 2|
            Q182CHI == 3|
            Q182PAK == 2|
            Q182RUS == 5|
            Q182UGA == 2|
            Q185INDIA == 2|
            USR == "R" ~ 1,
            TRUE ~ NA_real_
          ),
      
    #########################  
    ### SOCIAL DISTANCE 1 ###  
    #########################
    
      # original question number; question text; response options (input above)
        resp_soc_dist_1_qinfo = "NUM: Q37 [Q37A]; QTEXT: I'd like you to rate some different groups of people in (survey country) according to how you feel about them. For each group, please tell me whether your opinion is very favorable, mostly favorable, mostly unfavorable or very unfavorable. Jews.; ROPTIONS: 1 = Very favorable [=0] + 2 = Mostly favorable [=0] + 3 = Mostly unfavorable [=1] + 4 = Very unfavorable [=1]; TARGET: Jewish; TYPE: Favorability",
      
      # original response (as character vector)
        resp_soc_dist_1_original = 
          dplyr::recode(
            as.character(Q37A),
            "8" = NA_character_,
            "9" = NA_character_),       

      # binary recode (numeric: 1 = any negative attitude expressed; 0 otherwise)
        resp_soc_dist_1_bin_recode = 
          case_when(
            Q37A %in% c(1:2) ~ 0,
            Q37A %in% c(3:4) ~ 1,
            TRUE ~ NA_real_),
    
    #########################  
    ### SOCIAL DISTANCE 2 ###  
    #########################
    
      # original question number; question text; response options (input above)
        resp_soc_dist_2_qinfo = "NUM: Q37 [Q37C]; QTEXT: I'd like you to rate some different groups of people in (survey country) according to how you feel about them. For each group, please tell me whether your opinion is very favorable, mostly favorable, mostly unfavorable or very unfavorable. Muslims.; ROPTIONS: 1 = Very favorable [=0] + 2 = Mostly favorable [=0] + 3 = Mostly unfavorable [=1] + 4 = Very unfavorable [=1]; TARGET: Muslim; TYPE: Favorability",
      
      # original response (as character vector)
        resp_soc_dist_2_original = 
          dplyr::recode(
            as.character(Q37C),
            "8" = NA_character_,
            "9" = NA_character_),       

      # binary recode (numeric: 1 = any negative attitude expressed; 0 otherwise)
        resp_soc_dist_2_bin_recode = 
          case_when(
            Q37C %in% c(1:2) ~ 0,
            Q37C %in% c(3:4) ~ 1,
            TRUE ~ NA_real_),
    
    #########################  
    ### SOCIAL DISTANCE 3 ###  
    #########################
    
      # original question number; question text; response options (input above)
        resp_soc_dist_3_qinfo = "NUM: Q37UKR [Q37UKRB]; QTEXT: I'd like you to rate some different groups of people in (survey country) according to how you feel about them. For each group, please tell me whether your opinion is very favorable, mostly favorable, mostly unfavorable or very unfavorable. Jews.; ROPTIONS: 1 = Very favorable [=0] + 2 = Mostly favorable [=0] + 3 = Mostly unfavorable [=1] + 4 = Very unfavorable [=1]; TARGET: Jewish; TYPE: Favorability",
      
      # original response (as character vector)
        resp_soc_dist_3_original = 
          dplyr::recode(
            as.character(Q37UKRB),
            "8" = NA_character_,
            "9" = NA_character_),       

      # binary recode (numeric: 1 = any negative attitude expressed; 0 otherwise)
        resp_soc_dist_3_bin_recode = 
          case_when(
            Q37UKRB %in% c(1:2) ~ 0,
            Q37UKRB %in% c(3:4) ~ 1,
            TRUE ~ NA_real_),
    
    ############################  
    ### GENERAL SOCIAL TRUST ###  
    ############################
    
      # original question number; question text; response options (input above)
        resp_gentrust_qinfo = NA, # checked; no question asked

      # original response (as character vector)
        resp_gentrust_original = NA,       

      # binary recode (numeric: 1 = any negative attitude expressed; 0 otherwise)
        resp_gentrust_bin_recode = NA,    
    
    #########################  
    ##### RELIGIOSITY #######  
    #########################
    
      # original question number; question text; response options (input above)
        resp_religiosity_qinfo = "NUM: Q137; QTEXT: How often, if at all, do you pray:  hardly ever, only during religious holidays, only on Fridays, only on Fridays and religious holidays, more than once a week, every day at least once, or every day five times?; ROPTIONS: 1 = Hardly ever + 2 = Only during religious holidays + 3 = Only on Fridays + 4 = Only on Fridays and religious holidays + 5 = More than once a week + 6 = Every day at least once + 7 = Every day five times",
  
      # original response (as numeric vector, with non-substantive responses coded as NA_real_)
        resp_religiosity_original = # significant missingness; confirmed, no alternative question
          dplyr::recode(
            as.numeric(Q137),
            `8` = NA_real_,
            `9` = NA_real_),       

      # recode (numeric: scaled 0-1, where 1 is more religious)
        resp_religiosity_recode = (resp_religiosity_original - 1)/6
    
    ) %>% 
    select(starts_with("resp_"))
```

# Clean Pew Global Attitudes Spring 2015

```{r}
# declare religion variables
  rel_vars <- 
    c("Q78ARG", "Q78AUS", "Q78BRA", "Q78BRI", "Q78BUR", 
      "Q78CAN", "Q78CHL", "Q78CHI", "Q78FRA", "Q78GER", 
      "Q78GHA", "Q78IND", "Q78INDO", "Q78ISR", "Q78ITA", 
      "Q78JPN", "Q78JOR", "Q78KEN", "Q78LEB", "Q78MAL", 
      "Q78MEX", "Q78NIG", "Q78PAK", "Q78PAL", "Q78PER", 
      "Q78PHI", "Q78POL", "Q78RSA", "Q78RUS", "Q78SEN", 
      "Q78SKOR", "Q78SPA", "Q78TAN", "Q78TUR", "Q78UGA", 
      "Q78UKR", "Q78VEN", "Q78VIE", "Q78USA")

# declare dates
  dates_pew_gap_spring_15 <-
    tribble(
        ~resp_country_common, ~resp_interview_start_date, ~resp_interview_end_date,
        "Argentina", "Apr. 6, 2015", "Apr. 28, 2015",
        "Australia", "Apr. 7, 2015", "Apr. 21, 2015",
        "Brazil", "Apr. 7, 2015", "May. 4, 2015",
        "Burkina Faso", "Apr. 14, 2015", "May. 21, 2015",
        "Canada", "May. 4, 2015", "May. 15, 2015",
        "Chile", "Apr. 6, 2015", "May. 8, 2015",
        "China", "Apr. 15, 2015", "May. 27, 2015",
        "Ethiopia", "Mar. 25, 2015", "Apr. 17, 2015",
        "France", "Apr. 7, 2015", "Apr. 20, 2015",
        "Germany", "Apr. 13, 2015", "Apr. 29, 2015",
        "Ghana", "Apr. 9, 2015", "Apr. 29, 2015",
        "India", "Apr. 6, 2015", "May. 19, 2015",
        "Indonesia", "Apr. 8, 2015", "May. 4, 2015",
        "Israel", "Apr. 26, 2015", "May. 14, 2015",
        "Italy", "Apr. 7, 2015", "Apr. 24, 2015",
        "Japan", "Apr. 6, 2015", "Apr. 25, 2015",
        "Jordan", "Apr. 7, 2015", "Apr. 29, 2015",
        "Kenya", "Apr. 11, 2015", "May. 4, 2015",
        "Lebanon", "Apr. 7, 2015", "May. 5, 2015",
        "Malaysia", "Apr. 6, 2015", "May. 11, 2015",
        "Mexico", "Apr. 7, 2015", "Apr. 19, 2015",
        "Nigeria", "Apr. 20, 2015", "May. 12, 2015",
        "Pakistan", "Apr. 6, 2015", "Apr. 30, 2015",
        "Palestine", "Apr. 6, 2015", "May. 2, 2015",
        "Peru", "Apr. 7, 2015", "Apr. 29, 2015", 
        "Philippines", "Apr. 13, 2015", "Apr. 28, 2015",
        "Poland", "Apr. 7, 2015", "May. 13, 2015",
        "Russia", "Apr. 6, 2015", "Apr. 17, 2015",
        "Senegal", "Apr. 17, 2015", "May. 12, 2015",
        "South Africa", "Apr. 8, 2015", "Apr. 29, 2015",
        "South Korea", "Apr. 6, 2015", "Apr. 19, 2015",
        "Spain", "Apr. 7, 2015", "Apr. 23, 2015",
        "Tanzania", "Apr. 18, 2015", "May. 13, 2015",
        "Turkey", "Apr. 5, 2015", "May. 15, 2015",
        "Uganda", "Apr. 9, 2015", "Apr. 24, 2015",
        "Ukraine", "Apr. 14, 2015", "Apr. 30, 2015",
        "UK", "Apr. 8, 2015", "Apr. 28, 2015",
        "US", "Apr. 13, 2015", "May. 3, 2015",
        "Venezuela", "Apr. 6, 2015", "May. 6, 2015",
        "Vietnam", "Apr. 6, 2015", "May. 10, 2015"
      ) %>%
      mutate(
        resp_interview_start_date = as.Date(resp_interview_start_date, "%b. %d, %Y"),
        resp_interview_end_date = as.Date(resp_interview_end_date, "%b. %d, %Y"))

# clean pew global attitudes spring 2015
  n10_clean_pew_gap_spring_15 <-
    pew_gap_spring_15_raw %>% 
    mutate(
      
    #########################  
    ####### META-DATA #######  
    #########################      
      
      # source name (character vector, title case)
        resp_source = "Pew Global Attitudes Project",
        
      # round number (character vector, title case)  
        resp_round = "Spring 2015",
      
      # url to dataset source, where publicly available (character vector)
        resp_original_data_url = "pewrsr.ch/3wToJeP",

      # survey mode (in-person/phone/internet/mail), see https://www.pewresearch.org/methodology/international-survey-research/international-methodology/global-attitudes-survey/all-country/2015
        resp_survey_mode =
          case_when(
            COUNTRY %in% c(2, 9, 10, 16, 32, 37, 38) ~ "phone",
            TRUE ~ "in-person"),

      # country (character vector; list of countries as written in original source)
        resp_country_original = to_character(COUNTRY),

      # country (character vector; converts to countrycode county.name list)
        resp_country_common = 
          countryname(resp_country_original),
        
      # interview date (variable of class Date; if only month given, input 1st of month)
        resp_interview_date = as.Date(as.character(Q201), "%d%m%Y")) %>%
    left_join(dates_pew_gap_spring_15, by = "resp_country_common") %>%
    mutate(
   
    #########################  
    ##### DEMOGRAPHICS ######  
    #########################

      # respondent denomination (recoded jointly below)
        # note: no variable for ethiopia in the dataset, so fully missing
        Q78ARGB = to_character(Q78ARGB),
        Q78AUSB = to_character(Q78AUSB),
        Q78BRAB = to_character(Q78BRAB),
        Q78BRIB = to_character(Q78BRIB),
        Q78BURB = to_character(Q78BURB),
        Q78CANB = to_character(Q78CANB),
        Q78CHLB = to_character(Q78CHLB),
        Q78FRAB = to_character(Q78FRAB),
        Q78GERB = to_character(Q78GERB),
        Q78GHAB = to_character(Q78GHAB),
        Q78INDB = to_character(Q78INDB),
        Q78INDOB = to_character(Q78INDOB), 
        Q78ISRB = to_character(Q78ISRB),
        Q78ITAB = to_character(Q78ITAB),
        Q78JPNB = to_character(Q78JPNB),
        Q78JORB = to_character(Q78JORB),
        Q78KENB = to_character(Q78KENB),
        Q78LEBB = to_character(Q78LEBB),
        Q78MALB = to_character(Q78MALB),
        Q78MEXB = to_character(Q78MEXB),
        Q78NIGB = to_character(Q78NIGB),
        Q78PAKB = to_character(Q78PAKB),
        Q78PALB = to_character(Q78PALB),
        Q78PERB = to_character(Q78PERB),
        Q78PHIB = to_character(Q78PHIB),
        Q78POLB = to_character(Q78POLB),
        Q78RSAB = to_character(Q78RSAB),
        Q78RUSB = to_character(Q78RUSB),
        Q78SENB = to_character(Q78SENB),
        Q78SKORB = to_character(Q78SKORB), 
        Q78SPAB = to_character(Q78SPAB),
        Q78TANB = to_character(Q78TANB),
        Q78TURB = to_character(Q78TURB),
        Q78UGAB = to_character(Q78UGAB),
        Q78UKRB = to_character(Q78UKRB),
        Q78VENB = to_character(Q78VENB),
        Q78VIEB = to_character(Q78VIEB),

        Q78USA = Q78C, #usa, different variable naming
        Q78USAB = to_character(Q78D), #usa, different variable naming
        across(all_of(rel_vars), to_character)) %>% 
      mutate(
        Q78ARG = case_when(Q78ARGB == "Yes" ~ "Christian", TRUE ~ Q78ARG),
        Q78AUS = case_when(Q78AUSB == "Yes would" ~ "Christian", TRUE ~ Q78AUS),
        Q78BRA = case_when(Q78BRAB == "Yes" ~ "Christian", TRUE ~ Q78BRA),
        Q78BRI = case_when(Q78BRIB == "Yes, Christian" ~ "Christian", TRUE ~ Q78BRI),
        Q78CAN = case_when(Q78CANB == "Yes, Christian" ~ "Christian", TRUE ~ Q78CAN),
        Q78CHL = case_when(Q78CHLB == "Yes" ~ "Christian", TRUE ~ Q78CHL),
        Q78FRA = case_when(Q78FRAB == "Yes, Christian" ~ "Christian", TRUE ~ Q78FRA),
        Q78GER = case_when(Q78GERB == "Yes, Christian" ~ "Christian", TRUE ~ Q78GER),
        Q78GHA = case_when(Q78GHAB == "Christian" ~ "Christian", TRUE ~ Q78GHA),
        Q78ITA = case_when(Q78ITAB == "Yes, Christian" ~ "Christian", TRUE ~ Q78ITA),
        Q78JPN = case_when(Q78JPNB == "Yes, Buddhist" ~ "Buddhist", TRUE ~ Q78JPN),
        Q78JOR = case_when(Q78JORB == "Yes, Muslim" ~ "Muslim", TRUE ~ Q78JOR),
        Q78KEN = case_when(Q78KENB == "Christian" ~ "Christian", TRUE ~ Q78KEN),
        Q78MEX = case_when(Q78MEXB == "Yes" ~ "Christian", TRUE ~ Q78MEX),
        Q78NIG = case_when(Q78NIGB == "Christian" ~ "Christian", TRUE ~ Q78NIG),
        Q78PER = case_when(Q78PERB == "Yes" ~ "Christian", TRUE ~ Q78PER),
        Q78PHI = case_when(Q78PHIB == "Yes, Christian" ~ "Christian", TRUE ~ Q78PHI),
        Q78POL = case_when(Q78POLB == "Yes, Christian" ~ "Christian", TRUE ~ Q78POL),
        Q78RSA = case_when(Q78RSAB == "Yes" ~ "Christian", TRUE ~ Q78RSA),
        Q78SKOR = case_when(Q78SKORB == "Buddhist" ~ "Buddhist", TRUE ~ Q78SKOR), 
        Q78SPA = case_when(Q78SPAB == "Yes, Christian" ~ "Christian", TRUE ~ Q78SPA),
        Q78TAN = case_when(Q78TANB == "Christian" ~ "Christian", TRUE ~ Q78TAN),
        Q78UGA = case_when(Q78UGAB == "Christian" ~ "Christian", TRUE ~ Q78UGA),
        Q78UKR = case_when(Q78UKRB == "Yes, Christian" ~ "Christian", TRUE ~ Q78UKR),
        Q78VEN = case_when(Q78VENB == "Yes" ~ "Christian", TRUE ~ Q78VEN),
        Q78USA = case_when(Q78USAB == "Yes" ~ "Christian", TRUE ~ Q78USA),
        
        resp_denomination = coalesce(!!! select(., all_of(rel_vars))),         

      # respondent's age (character vector; bins denoted by single dash ["18-25"])
        resp_age =
          dplyr::recode(
            as.character(Q146),
            "97" = "97+",
            "98" = NA_character_,
            "99" = NA_character_),        
      
      # respondent's education level
        resp_education_original =
          case_when(
            #Argentina #In Spanish
            Q163ARG == 1 ~ "1. Sin educación formal (preescolar o menos)/No formal education (preschool or younger) [No education]",
            Q163ARG == 2 ~ "2. Primaria incompleta (Educacion General B sica (EGB), 1.& y 2.& ciclo)/Incomplete primary (Basic General Education (EGB), 1st and 2nd cycle) [No education]",
            Q163ARG == 3 ~ "3. Primaria completa (EGB, 1.& y 2.& ciclo)/Complete primary (EGB, 1st and 2nd cycle) [Primary]",
            Q163ARG == 4 ~ "4. Secundaria incompleta/Incomplete secondary [Primary]",
            Q163ARG == 5 ~ "5. Secundaria completa (EGB, 3.& ciclo)/Complete secondary (EGB, 3rd cycle) [Primary]",
            Q163ARG == 6 ~ "6. Secundaria de nivel superior incompleta (incluye 8.& y 9.& a/o de EGB; 1.& y 2.& a/o de Polimodal)/Incomplete upper secondary (includes 8th and 9th year of EGB; 1st and 2nd year of Polimodal) [Primary]",
            Q163ARG == 7 ~ "7. Secundaria de nivel superior completa(incluye 3.& a/o de Polimodal)/Complete upper secondary (includes 3.& a/o Polimodal) [Primary]",
            Q163ARG == 8 ~ "8. Terciaria incompleta/Incomplete tertiary [Primary]",
            Q163ARG == 9 ~ "9. Universitaria incompleta/Incomplete university [Primary]",
            Q163ARG == 10 ~ "10. Terciaria completa/Complete tertiary [Primary]", #assumed given position in list
            Q163ARG == 11 ~ "11. Universitaria completa/Complete university [College]",
            Q163ARG == 12 ~ "12. Posgrados (Maestrja or doctorado)/Postgraduate (Master's or doctorate) [College]",
            Q163ARG %in% c(98, 99) ~ NA_character_,
            #Australia
            Q163AUS == 1 ~ "1. Pre-Primary school or less (Preschool or Kindergarten) [No education]",
            Q163AUS == 2 ~ "2. Primary school [Primary]",
            Q163AUS == 3 ~ "3. Some secondary/ technical [Primary]",
            Q163AUS == 4 ~ "4. School certificate/ intermediate/ 4th form/ year 10/ junior/ achievement certificate (junior/lower secondary school) [Primary]",
            Q163AUS == 5 ~ "5. 5th form/ year 11/ leaving/ sub-senior certificate [Primary]",
            Q163AUS == 6 ~ "6. Finished matriculation/ higher school certificate (HSC) or (VCE)/ year 12/ senior certificate (senior/upper secondary school) [Primary]",
            Q163AUS == 7 ~ "7. Some TAFE/now at TAFE [Primary]",
            Q163AUS == 8 ~ "8. Finished technical school/ TAFE/ other certificate or apprenticeship [Primary]",
            Q163AUS == 9 ~ "9. Some university/ now at university [Primary]",
            Q163AUS == 10 ~ "10. Diploma from college of advanced education/ TAFE/ management training including diploma [Primary]",
            Q163AUS == 11 ~ "11. Degree from university/ college of advanced education [College]",
            Q163AUS == 12 ~ "12. Graduate Diploma / Masters [College]",
            Q163AUS == 13 ~ "13. Ph.D [College]",
            Q163AUS %in% c(98, 99) ~ NA_character_,
            #Brazil
            Q163BRA == 1 ~ "1. Não recebeu educação formal/No formal education [No education]",
            Q163BRA == 2 ~ "2. Primário/ Fundamental (1ª a 4ª série) incompleto/Primary/Elementary (1st to 4th grade) incomplete [No education]",
            Q163BRA == 3 ~ "3. Primário/ Fundamental (1ª a 4ª série) completo/Primary/Elementary (1st to 4th grade) complete [Primary]",
            Q163BRA == 4 ~ "4. Ginasial/ Fundamental (5ª a 8ª/9ª série) incompleto/Junior High / Elementary (5th to 8th/9th grade) incomplete [Primary]",
            Q163BRA == 5 ~ "5. Ginasial/ Fundamental (5ª a 8ª/9ª série) completo/Junior High / Elementary (5th to 8th/9th grade) complete [Primary]",
            Q163BRA == 6 ~ "6. Colegial/ Ensino Médio incompleto/High school/Incomplete high school [Primary]",
            Q163BRA == 7 ~ "7. Colegial/ Ensino médio completo/High school/Completed high school [Primary]",
            Q163BRA == 8 ~ "8. Curso superior incompleto (bacharelado, licenciatura ou tecnólogo)/Incomplete higher education (bachelor's, licensure or technologist) [Primary]",
            Q163BRA == 9 ~ "9. Curso superior de bacharelado ou licenciatura completo/Complete higher education (bachelor's, licensure or technologist) [College]",
            Q163BRA == 10 ~ "10. Pós-graduação lato sensu completo (especialização, mestrado profissional, MBA)/Complete lato sensu postgraduate degree (specialization, professional master's, MBA) [College]",
            Q163BRA == 11 ~ "11. Mestrado completo (stricto sensu, acadêmico)/Complete Master's (stricto sensu, academic) [College]",
            Q163BRA == 12 ~ "12. Doutorado completo/Complete Doctorate [College]",
            Q163BRA %in% c(98, 99) ~ NA_character_,
            #Burkina Faso
            Q163BUR == 1 ~ "1. No formal schooling [No education]",
            Q163BUR == 2 ~ "2. Preschool [No education]",
            Q163BUR == 3 ~ "3. Primary school (CEP) [Primary]",
            Q163BUR == 4 ~ "4. General secondary school, first cycle (BEPC) / Junior High School [Primary]",
            Q163BUR == 5 ~ "5. Technical and professional secondary school, first cycle (CAP) / Vocational training [Primary]",
            Q163BUR == 6 ~ "6. General secondary school, second cycle (BAC) / Technical secondary school, second cycle / High School [Primary]",
            Q163BUR == 7 ~ "7. Vocational secondary school, second cycle (BAC vocational or BEP) / Vocational training [Primary]",
            Q163BUR == 8 ~ "8. Post-secondary vocational training / Vocational diploma [Primary]", #check
            Q163BUR == 9 ~ "9. Higher education (DEUG, Masters, DESS, Licence) / Vocational training (Engineering) [College]",
            Q163BUR == 10 ~ "10. Higher education (DUT/BTS) [College]",
            Q163BUR == 11 ~ "11. Higher education (DEA/Doctorate) [College]",
            Q163BUR == 98 ~ NA_character_,
            #Britain
            Q163BRI == 1 ~ "1. No qualifications obtained [Primary]",
            Q163BRI == 2 ~ "2. Primary school / infant & junior schools [Primary]",
            Q163BRI == 3 ~ "3. GCSE / SCE Standard Grades GNVQ Intermediate SQA National Courses, NVQ1, NVQ 2 [Primary]",
            Q163BRI == 4 ~ "4. A-levels; S-levels; A2-level; GCE Applied A-level; NVQ/SVQ Levels 1-3; Scottish Highers; Scottish SCE/SLC/SUPE at Higher [Primary]",
            Q163BRI == 5 ~ "5. Nursing certificate [Primary]",
            Q163BRI == 6 ~ "6. Foundation degree/HND/Higher Education Diploma/NVQ 4, NVQ5 [Primary]",
            Q163BRI == 7 ~ "7. University (Bachelor) [College]",
            Q163BRI == 8 ~ "8. University (Masters degree) [College]",
            Q163BRI == 9 ~ "9. University (Doctorate, PHD) [College]",
            Q163BRI %in% c(98, 99) ~ NA_character_,
            #Canada
            Q163CAN == 1 ~ "1. No formal schooling (Preschool or Kindergarten) [No education]",
            Q163CAN == 2 ~ "2. Completed elementary school (Grade 6) [Primary]",
            Q163CAN == 3 ~ "3. Some junior high school (Grades 7) [Primary]",
            Q163CAN == 4 ~ "4. Completed junior high school (Grade 8) [Primary]",
            Q163CAN == 5 ~ "5. Some senior high school or vocational secondary school (Grade 9-10 in Quebec/Grade 9-11 in the rest of Canada) [Primary]",
            Q163CAN == 6 ~ "6. Completed secondary school or vocational secondary school (secondary school diploma) (Grade 11 in Quebec/Grade 12 in res [Primary]",
            Q163CAN == 7 ~ "7. College preliminary year/Trade certificate/upgrading certificate [Primary]",
            Q163CAN == 8 ~ "8. Undergraduate qualifying program/University Transfer Program (CEGEP in Quebec) [Primary]",
            Q163CAN == 9 ~ "9. Technical diploma/community college diploma or certificate [Primary]",
            Q163CAN == 10 ~ "10. Four year college or university degree/Bachelor’s degree (e.g., BS, BA, AB) [College]",
            Q163CAN == 11 ~ "11. Postgraduate or professional degree, including master’s degree (e.g., MA, MS, PhD, MD, DESS, graduate school) [College]",
            Q163CAN %in% c(98, 99) ~ NA_character_,
            #Chile
            Q163CHL == 1 ~ "1. Analfabeto/Illiterate [No education]",
            Q163CHL == 2 ~ "2. Ninguno, no estudio/None, I don't study [No education]",
            Q163CHL == 3 ~ "3. 1º básico (preparatoria)/1st grade (preparatory) [No education]",
            Q163CHL == 4 ~ "4. 2º básico/2nd grade [No education]",
            Q163CHL == 5 ~ "5. 3º básico/3rd grade [No education]",
            Q163CHL == 6 ~ "6. 4º básico/4th grade [No education]",
            Q163CHL == 7 ~ "7. 5º básico/5th grade [Primary]",
            Q163CHL == 8 ~ "8. 6º básico/6th grade [Primary]",
            Q163CHL == 9 ~ "9. 7º básico, primero de humanidades/7th grade, first of humanities [Primary]",
            Q163CHL == 10 ~ "10. 8º básico, segundo de humanidades/8th grade, second in humanities [Primary]",
            Q163CHL == 11 ~ "11. 1º enseñanza media, tercero de humanidades/1st high school, 3rd humanities [Primary]",
            Q163CHL == 12 ~ "12. 2º enseñanza media, cuarto de humanidades/2nd high school, fourth of humanity [Primary]",
            Q163CHL == 13 ~ "13. 3º enseñanza media, quinto de humanidades/3rd high school, fifth in humanities [Primary]",
            Q163CHL == 14 ~ "14. 4º enseñanza media, sexto de humanidades/4th high school, sixth in humanities [Primary]",
            Q163CHL == 15 ~ "15. Técnico-profesional completo/Complete technical-professional [Primary]",
            Q163CHL == 16 ~ "16. Educación universitaria, incompleta/College education, incomplete [Primary]",
            Q163CHL == 17 ~ "17. Educación universitaria, bachelor completo/University education, bachelor complete [College]",
            Q163CHL == 18 ~ "18. Educación universitaria, Master completa/University education, Master complete [College]",
            Q163CHL == 19 ~ "19. Postgrado, Doctorado/Postgraduate, Doctorate [College]",
            Q163CHL %in% c(98, 99) ~ NA_character_,
            #China
            Q163CHI == 1 ~ "1. No education [No education]",
            Q163CHI == 2 ~ "2. Incomplete primary education [No education]",
            Q163CHI == 3 ~ "3. Primary school [Primary]",
            Q163CHI == 4 ~ "4. Junior secondary school [Primary]",
            Q163CHI == 5 ~ "5. Secondary Vocational School/Technical secondary school [Primary]",
            Q163CHI == 6 ~ "6. Senior High School [Primary]",
            Q163CHI == 7 ~ "7. Higher technical & vocational School/Junior College [Primary]",
            Q163CHI == 8 ~ "8. Bachelor (Undergraduate) [College]",
            Q163CHI == 9 ~ "9. Master [College]",
            Q163CHI == 10 ~ "10. Doctorate [College]",
            Q163CHI %in% c(98, 99) ~ NA_character_,
            #Ethiopia
            Q163ETH == 1 ~ "1. No formal education [No education]",
            Q163ETH == 2 ~ "2. Some primary [No education]",
            Q163ETH == 3 ~ "3. Primary school [Primary]",
            Q163ETH == 4 ~ "4. Some secondary [Primary]",
            Q163ETH == 5 ~ "5. Secondary completed [Primary]",
            Q163ETH == 6 ~ "6. Partial preparatory [Primary]",
            Q163ETH == 7 ~ "7. Completed preparatory [Primary]",
            Q163ETH == 8 ~ "8. Some mid-level college (not university) [Primary]",
            Q163ETH == 9 ~ "9. Completed mid-level college (not university) [Primary]",
            Q163ETH == 10 ~ "10. Some university [Primary]",
            Q163ETH == 11 ~ "11. Undergraduate degree [College]",
            Q163ETH == 12 ~ "12. Postgraduate degree (Master) [College]",
            Q163ETH == 13 ~ "13. Postgraduate degree (PhD) [College]",
            Q163ETH %in% c(98, 99) ~ NA_character_,
            #France
            Q163FRA == 1 ~ "1. Non scolarisé ou école primaire non achevée/No schooling or primary school not complete [No education]",
            Q163FRA == 2 ~ "2. Ecole primaire avec le CEP/Primary school with CEP [Primary]",
            Q163FRA == 3 ~ "3. Enseignement du premier cycle du second degré – Collège (BREVET)/Education of the first grade of the second degree - College (BREVET) [Primary]",
            Q163FRA == 4 ~ "4. Enseignement de second cycle professionnel du second degré (sous statut scolaire et apprentissage) (C.A.P, B.E.P, etc)/Education of the second grade of the second degree (under school status and apprenticeship) (C.A.P, B.E.P, etc) [Primary]",
            Q163FRA == 5 ~ "5. Enseignement de second cycle général du second degré (Baccalauréat général et technologique)/General secondary education of the second degree (General and technological baccalaureate) [Primary]",
            Q163FRA == 6 ~ "6. Enseignement des écoles sociales et autres formations (Diplôme de moniteur éducateur, éducateur technique spécialisé)/Education of social schools and other training (Diploma of educator monitor, specialized technical educator) [Primary]",
            Q163FRA == 7 ~ "7. Préparation à l'entrée d'une formation sélective de niveau 5B (Prépa entrée écoles paramédicales et sociales, classes de)/Preparation for entry into selective level 5B training (Preparation for entry to paramedical and social schools) [Primary]",
            Q163FRA == 8 ~ "8. Enseignement pré-universitaire (Diplôme de la capacité en droit, diplôme d'accès aux études universitaires)/Pre-university education (Diploma of capacity in law, diploma of access to university studies) [Primary]",
            Q163FRA == 9 ~ "9. Diplôme universitaire de technologie (DUT) // Brevet de technicien supérieur (BTS), Diplôme des métiers d'art (DMA)/University diploma in technology (DUT) // Higher technician certificate (BTS), Diploma in crafts (DMA) [Primary]", #check
            Q163FRA == 10 ~ "10. Enseignement de premier cycle des études universitaires (Diplôme d'études universitaires générales (DEUG), LICENCE, LICE/Undergraduate education of university studies (Diploma of general university studies (DEUG), LICENCE, LICE [College]",
            Q163FRA == 11 ~ "11. Enseignement de deuxième cycle des études universitaires (MAITRISE, Diplome (DESS), Master (LMD), Diplome Ingenieur)/Second-degree education of university studies (MASTER'S, Diplome (DESS), Master's (LMD), Diplome Ingenieur) [College]",
            Q163FRA == 12 ~ "12. Enseignement de troisième cycle des études universitaires  (Diplôme d'études approfondies (DEA), Diplome de docteur)/Postgraduate education of university studies (Diploma of advanced studies (DEA), Doctor's degree) [College]",
            Q163FRA %in% c(98, 99) ~ NA_character_,
            #Germany
            Q163GER == 1 ~ "1. kein Schulabschluss / Grundschule/no school certificate / elementary school [Primary]",#check
            Q163GER == 2 ~ "2. Volks- oder Hauptschule/Elementary or secondary school [Primary]",
            Q163GER == 3 ~ "3. Weiterführende Schule ohne Abitur/Secondary school with Abitur [Primary]",
            Q163GER == 4 ~ "4. Abitur, Fachhochschulreife, Hochschulreife/Abitur, technical college entrance qualification, university entrance qualification [Primary]",
            Q163GER == 5 ~ "5. abgeschlossenes Bachelor-Studium/ Grundstudium/ Vordiplom/ Berufsakademie, nicht abgeschl. Haupt- bzw. Master-Studium/Completed bachelor’s degree/ basic studies/ intermediate diploma/ professional academy, not completed Main or Master's degree [College]",
            Q163GER == 6 ~ "6. abgeschlossenes Haupt- bzw. Master-Studium/Completed main or Master's degree [College]",
            Q163GER == 7 ~ "7. Promotion, Doktorat/Promotion, Doctorate [College]",
            Q163GER %in% c(98, 99) ~ NA_character_,
            #Ghana
            Q163GHA == 1 ~ "1. No formal education [No education]",
            Q163GHA == 2 ~ "2. Kindergarten / Incomplete primary school [No education]",
            Q163GHA == 3 ~ "3. Primary school/incomplete junior high school (BECE) [Primary]",
            Q163GHA == 4 ~ "4. Junior high school (BECE) [Primary]",
            Q163GHA == 5 ~ "5. Technical and vocational education [Primary]",
            Q163GHA == 6 ~ "6. Senior high school (WASCE) [Primary]",
            Q163GHA == 7 ~ "7. Incomplete SSS/Vocational/Technical/ School [Primary]",
            Q163GHA == 8 ~ "8. Polytechnics non-tertiary programmes (DBS, BA) and professional bodies programme (certification) [Primary]",
            Q163GHA == 9 ~ "9. First stage of tertiary education (Bachelor) or Teacher training [College]",
            Q163GHA == 10 ~ "10. Master [College]",
            Q163GHA == 11 ~ "11. Secondary stage of tertiary education (PhD) [College]",
            Q163GHA %in% c(98, 99) ~ NA_character_,
            #India
            Q163INDIA == 1 ~ "1. Illiterate [No education]",
            Q163INDIA == 2 ~ "2. Literate but no formal schooling [No education]",
            Q163INDIA == 3 ~ "3. Primary Education (primary certificate) [Primary]",
            Q163INDIA == 4 ~ "4. Upper Primary (Upper Primary Certificate) [Primary]",
            Q163INDIA == 5 ~ "5. High School; Industrial Training Institute (matriculation certificate or ITI certificate [Primary]",
            Q163INDIA == 6 ~ "6. Senior Secondary / intermediate (Senior Secondary School Leaving Certificate) [Primary]",
            Q163INDIA == 7 ~ "7. Technical Education Training; Junior teachers training (diploma) [Primary]",
            Q163INDIA == 8 ~ "8. Tertiary, technical higher education (diploma / bachelor) [Primary]", 
            Q163INDIA == 9 ~ "9. Bachelor (university 1st) [College]",
            Q163INDIA == 10 ~ "10. Master (university  2nd) [College]",
            Q163INDIA == 11 ~ "11. Doctor (Tertiary 2nd) [College]",
            Q163INDIA %in% c(98, 99) ~ NA_character_,
            #Indonesia
            Q163INDO == 1 ~ "1. No formal schooling (Preschool or Kindergarten) [No education]",
            Q163INDO == 2 ~ "2. Did not finish elementary school (grade 1-5 SD) or Madrasah Ibtidaiyah [No education]",
            Q163INDO == 3 ~ "3. Finished elementary school (finishing class 6 SD) or Madrasah Ibtidaiyah [Primary]",
            Q163INDO == 4 ~ "4. Did not finish first school (class 7-8, SMP); Incomplete junior high school (completed secondary grades 7-8), Madrasah T [Primary]",
            Q163INDO == 5 ~ "5. Finished Junior High School (SMP, class 9), Madrasah Tsanawiyah or SMP Kejuruan [Primary]",
            Q163INDO == 6 ~ "6. Did not finish high school (class 10-11, SMA); Incomplete high school (completed high school grades 10-11), Madrasah Ali [Primary]",
            Q163INDO == 7 ~ "7. Finish High School (class 12, SMA), Madrasah Aliyah or Sekolah Menengah Kejuruan [Primary]",
            Q163INDO == 8 ~ "8. Diploma programmes (DI thru DIV) [Primary]",
            Q163INDO == 9 ~ "9. Program Sarjana (Graduate diploma (SI)) [College]",
            Q163INDO == 10 ~ "10.Program Master (Master degree (S2)) [College]",
            Q163INDO == 11 ~ "11. Program Doktor; Specialist S3 program [College]",
            Q163INDO %in% c(98, 99) ~ NA_character_,
            #Italy
            Q163ITA == 1 ~ "1. Senza titoli di studi/Without educational qualifications [No education]",
            Q163ITA == 2 ~ "2. Scuola primaria (scuola elementare)/Primary school (elementary school) [Primary]",
            Q163ITA == 3 ~ "3. Scuola secondaria di primo grado (scuola media)/Lower secondary school (secondary school) [Primary]",
            Q163ITA == 4 ~ "4. Scuola secondaria di secondo grado (scuola superiore)/Second grade secondary school (high school) [Primary]",
            Q163ITA == 5 ~ "5. Specializzazione professionale post-diploma (istruzione professionale successiva al diploma di scuola superiore)/Post-diploma professional specialization (vocational education after high school graduation) [Primary]",
            Q163ITA == 6 ~ "6. Laurea triennale/Three-year degree [College]",
            Q163ITA == 7 ~ "7. Laurea magistrale/Master's degree [College]",
            Q163ITA == 8 ~ "8. Specializzazione post-laurea/Postgraduate specialization [College]",
            Q163ITA %in% c(9, 10, 98, 99) ~ NA_character_,
            #Israel
            Q163ISR == 1 ~ "1. No formal education [No education]",
            Q163ISR == 2 ~ "2. Elementary school (Completed grades 1 to 6) [Primary]",
            Q163ISR == 3 ~ "3. Middle school (Completed grades 7 to 9) [Primary]",
            Q163ISR == 4 ~ "4. (Hebrew only) Partial Yeshiva [Primary]",
            Q163ISR == 5 ~ "5. High school (Grades 10 to 12) without Matriculation [Primary]",
            Q163ISR == 6 ~ "6. High school (Grades 10 to 12) with Matriculation [Primary]",
            Q163ISR == 7 ~ "7. (Hebrew only) Yeshiva without Matriculation [Primary]",
            Q163ISR == 8 ~ "8. (Hebrew only) Yeshiva with Matriculation [Primary]",
            Q163ISR == 9 ~ "9. Preparatory programmes for practical engineers (matriculation certificate) [Primary]",
            Q163ISR == 10 ~ "10. Pre-academic preparatory programmes (matriculation certificate) [Primary]",
            Q163ISR == 11 ~ "11. Post-secondary (technician, engineer) teacher training (teacher diploma) [Primary]",
            Q163ISR == 13 ~ "13. BA [College]",
            Q163ISR == 14 ~ "14. MA [College]",
            Q163ISR == 15 ~ "15. PhD [College]",
            Q163ISR %in% c(98, 99) ~ NA_character_,
            #Japan
            Q163JPN == 1 ~ "1. Did not receive any education / Kindergarten (Yochien) [No education]",
            Q163JPN == 2 ~ "2. Elementary school (Shogakko) [Primary]",
            Q163JPN == 3 ~ "3. Lower secundary school (Chuto-kyoiku-gakko (Zenki katei); Chugakko [Primary]",
            Q163JPN == 4 ~ "4. College of technology [Primary]",
            Q163JPN == 5 ~ "5. Upper Secondary school [Primary]",
            Q163JPN == 6 ~ "6. Junior college (associate degree), special training college (diploma = senmonshi or advanced diploma = kodo-senmonshi) [Primary]",
            Q163JPN == 7 ~ "7. Bachelor's degree (Gakushi) [College]",
            Q163JPN == 8 ~ "8. Master's degree (Shushi) or Juris Doctor (Homuhakushi) [College]",
            Q163JPN == 9 ~ "9. Doctors degree (Hakushi) [College]",
            Q163JPN %in% c(98, 99) ~ NA_character_,
            #Jordan
            Q163JOR == 1 ~ "1. No formal education [No education]",
            Q163JOR == 2 ~ "2. Incomplete basic education [No education]",
            Q163JOR == 3 ~ "3. Basic education, 1st cycle (completed less than 9 grades) [Primary]",
            Q163JOR == 4 ~ "4. Basic education, 2nd cycle (completed 9 grades) [Primary]", 
            Q163JOR == 5 ~ "5. Applied secondary education (applied secondary certificate) [Primary]",
            Q163JOR == 6 ~ "6. Comprehensive secondary education (secondary education certificate) [Primary]",
            Q163JOR == 7 ~ "7. Community college (Community college diploma and success in the comprehensive exam) [Primary]",
            Q163JOR == 8 ~ "8. Bachelor [College]",
            Q163JOR == 9 ~ "9. Higher diploma programme (higher diploma) [College]",
            Q163JOR == 10 ~ "10. Masters [College]",
            Q163JOR == 11 ~ "11. Doctorate [College]",
            Q163JOR %in% c(98, 99) ~ NA_character_,
            #Kenya
            Q163KEN == 1 ~ "1. No formal education [No education]",
            Q163KEN == 2 ~ "2. Early childhood development programme (ECD) [No education]",
            Q163KEN == 3 ~ "3. First stage of primary education (Standards 1 to 6) [Primary]",
            Q163KEN == 4 ~ "4. Second stage of primary education (Standards 7 to 8) (KCPE) [Primary]",
            Q163KEN == 5 ~ "5. Youth polytechnics programme (Craft trade certificate) [Primary]",
            Q163KEN == 6 ~ "6. Youth polytechnics programme (Technical vocational certificate) [Primary]",
            Q163KEN == 7 ~ "7. Incomplete secondary school [Primary]",
            Q163KEN == 8 ~ "8. Secondary education (KCSE) [Primary]",
            Q163KEN == 9 ~ "9. High School (Form 6) A level Certificate [Primary]",
            Q163KEN == 10 ~ "10. Technical and industrial vocational education training (TIVET) [Primary]",
            Q163KEN == 11 ~ "11. Teacher training; Polytechnics programmes [Primary]",
            Q163KEN == 12 ~ "12. Bachelor [College]",
            Q163KEN == 13 ~ "13. Masters [College]",
            Q163KEN == 14 ~ "14. Doctorate [College]",
            Q163KEN %in% c(98, 99) ~ NA_character_,
            #Lebanon
            Q163LEB == 1 ~ "1. No formal schooling (Preschool or Kindergarten) [No education]",
            Q163LEB == 2 ~ "2. Primary (1st and 2nd cycle primary education) [Primary]",
            Q163LEB == 3 ~ "3. Professional certificate or Certificate Professional (CAP, BP) [Primary]",
            Q163LEB == 4 ~ "4. Moyen, 3rd cycle (BREVET, Senior Education) [Primary]",
            Q163LEB == 5 ~ "5. Professional secondary (general Baccalaureate (SG)) [Primary]",
            Q163LEB == 6 ~ "6. Bachelor of Technology (Bachelor of Technology (BT)) [Primary]", 
            Q163LEB == 7 ~ "7. Secondary (general Baccalaureate (S. G.)) [Primary]",
            Q163LEB == 8 ~ "8. Superior Técnical (T.S.); Specialized diploma (D.E.S.); Technical education license (L.E.T.) [Primary]",
            Q163LEB == 9 ~ "9. 1st Master, License [College]",
            Q163LEB == 10 ~ "10. 2nd Master; Postgraduate Diploma (D.E.A.); MD [College]",
            Q163LEB == 11 ~ "11. Ph.D. [College]",
            Q163LEB %in% c(98, 99) ~ NA_character_,
            #Malaysia
            Q163MAL == 1 ~ "1. No formal education [No education]",
            Q163MAL == 2 ~ "2. Primary school (Standard 1 to Standard 6 / UPSR) [Primary]",
            Q163MAL == 3 ~ "3. Lower secondary school (Form 1 to Form 3 / PMR / SRP / LCE) [Primary]",
            Q163MAL == 4 ~ "4. Vocational / technical (vocational / technical school, e.g. SPVM / MCVE) [Primary]",
            Q163MAL == 5 ~ "5. Upper secondary school (Form 4 to Form 5 / SPM / MCE / SC / GCE ‘O’ Level) [Primary]",
            Q163MAL == 6 ~ "6. Trade and technical skills institution  (Studies in trade and technical skills institution, e.g. certificate from SKM) [Primary]",
            Q163MAL == 7 ~ "7.  Post Secondary (Post-secondary studies, e.g. STPM / STP / STA / 4THANAWI / HSC / GCE ‘A’ Level, diploma) [Primary]",
            Q163MAL == 8 ~ "8. Tertiary (Tertiary studies, e.g. post-diploma / college / polytechnic / university education) [College]",
            Q163MAL == 9 ~ "9. Post graduate Master / post graduate diploma [College]",
            Q163MAL == 10 ~ "10. Post graduate (PhD) [College]",
            Q163MAL %in% c(98, 99) ~ NA_character_,
            #Mexico
            Q163MEX == 1 ~ "1. Sin educación formal/No formal education [No education]",
            Q163MEX == 2 ~ "2. Escuela primaria incompleta/Incomplete primary school [No education]",
            Q163MEX == 3 ~ "3. Escuela primaria completa/Complete primary school [Primary]",
            Q163MEX == 4 ~ "4. Educación secundaria incompleta/Incomplete secondary education [Primary]",
            Q163MEX == 5 ~ "5. Educación secundaria completa/Complete secondary education [Primary]",
            Q163MEX == 6 ~ "6. Educación técnica posterior a Secundaria/Post-Secondary Technical Education [Primary]",
            Q163MEX == 7 ~ "7. Bachillerato general o tecnológico/General or technological high school [Primary]",
            Q163MEX == 8 ~ "8. Educación técnica posterior a Bachillerato/Post-Baccalaureate Technical Education [Primary]",
            Q163MEX == 9 ~ "9. Algunos estudios universitarios, incompletos/Some college studies, incomplete [Primary]",
            Q163MEX == 10 ~ "10. Licenciatura universitaria, Educación normal, Licenciatura tecnológica/University degree, Normal education, Technological degree [College]",
            Q163MEX == 11 ~ "11. Maestría/Master's [College]",
            Q163MEX == 12 ~ "12. Doctorado/Doctorate [College]",
            Q163MEX %in% c(98, 99) ~ NA_character_,
            #Nigeria
            Q163NIG == 1 ~ "1. Pre-primary education [No education]",
            Q163NIG == 2 ~ "2. Primary education [Primary]",
            Q163NIG == 3 ~ "3. Junior secondary [Primary]",
            Q163NIG == 4 ~ "4. Senior secondary [Primary]",
            Q163NIG == 5 ~ "5. Secondary technical schools programmes [Primary]",
            Q163NIG == 6 ~ "6. National Diploma, NCE [Primary]",
            Q163NIG == 7 ~ "7. Higher National Diploma [Primary]",
            Q163NIG == 8 ~ "8. Bachelor [College]",
            Q163NIG == 9 ~ "9. Master [College]",
            Q163NIG == 10 ~ "10. PhD [College]",
            Q163NIG %in% c(98, 99) ~ NA_character_,
            #Pakistan
            Q163PAK == 1 ~ "1. No education at all / Illiterate [No education]",
            Q163PAK == 2 ~ "2. No formal education but can read/write simple Urdu [No education]",
            Q163PAK == 3 ~ "3. Incomplete primary education (completed less than 5 grades) [No education]",
            Q163PAK == 4 ~ "4. Complete primary education (completed 5 grades) [Primary]",
            Q163PAK == 5 ~ "5. Incomplete middle school (completed 6 or 7 grades) [Primary]",
            Q163PAK == 6 ~ "6. Complete middle school (completed 8 grades) [Primary]",
            Q163PAK == 7 ~ "7. High level (IX-X) (Secondary school certificate or Matriculation) [Primary]",
            Q163PAK == 8 ~ "8. Higher secondary or Intermediate (Grades XI - XII) (Intermediate Certificate or Higher secondary certificate) [Primary]",
            Q163PAK == 9 ~ "9. Primary teaching certificate (PTC); Technical education (diploma); Nursing (Certificate and Diploma; RN) [Primary]",
            Q163PAK == 10 ~ "10. Bachelor (2 year) [Primary]",
            Q163PAK == 11 ~ "11. Bachelor (4 year) [College]",
            Q163PAK == 12 ~ "12. Master, Medical degree [College]",
            Q163PAK == 13 ~ "13. Doctorate [College]",
            Q163PAK %in% c(98, 99) ~ NA_character_,
            #Palestine
            Q163PAL == 1 ~ "1. No formal education / Kindergarten [No education]",
            Q163PAL == 2 ~ "2. Incomplete elementary education (completed less than 5 grades under new system or less than 6 grades under old system) [No education]",
            Q163PAL == 3 ~ "3. Complete elementary education (completed 5 grades under new system or 6 grades under old system) [Primary]",
            Q163PAL == 4 ~ "4. Incomplete preparatory education (completed less than 10 grades under new system or less than 9 grades under old system) [Primary]",
            Q163PAL == 5 ~ "5. Complete preparatory education (completed 10 grades under new system or 9 grades under old system) [Primary]",
            Q163PAL == 6 ~ "6. Incomplete secondary education (completed 11 grades) [Primary]",
            Q163PAL == 7 ~ "7. Secondary Vocational (completed 12 grades) [Primary]",
            Q163PAL == 8 ~ "8. Complete secondary education (completed 12 grades) [Primary]",
            Q163PAL == 9 ~ "9. Diploma // Vocational training [Primary]",
            Q163PAL == 10 ~ "10. Bachelor [College]",
            Q163PAL == 11 ~ "11. Master [College]",
            Q163PAL == 12 ~ "12. PhD [College]",
            Q163PAL %in% c(98, 99) ~ NA_character_,
            #Peru
            Q163PER == 1 ~ "1. Sin educación/ Educación Inicial (Educación Inicial 3 a 5 años, escolarizada)/No education / Kindergarten (Preschool 3 to 5 years old) [No education]",
            Q163PER == 2 ~ "2. Primaria incompleta/Primary education incomplete [No education]",
            Q163PER == 3 ~ "3. Primaria completa/Primary education complete [Primary]",
            Q163PER == 4 ~ "4. Secundaria incompleta/Secondary education incomplete [Primary]",
            Q163PER == 5 ~ "5. Secundaria completa/Secondary education complete [Primary]",
            Q163PER == 6 ~ "6. Superior no-universitaria/University incomplete [Primary]",
            Q163PER == 7 ~ "7. Superior Univ. Incompleta/University incomplete [Primary]",
            Q163PER == 8 ~ "8. Superior universitaria; Escuelas de Oficiales completa/University superior; Full Officer Schools [College]",
            Q163PER == 9 ~ "9. Maestría/Master’s degree [College]",
            Q163PER == 10 ~ "10. Doctorado/PhD’s degree [College]",
            Q163PER %in% c(98, 99) ~ NA_character_,
            #Philippines
            Q163PHI == 1 ~ "1. No formal education [No education]",
            Q163PHI == 2 ~ "2. Incomplete elementary [No education]",
            Q163PHI == 3 ~ "3. Finished elementary [Primary]",
            Q163PHI == 4 ~ "4. Incomplete high school (Secondary, General (years 1-3) [Primary]",
            Q163PHI == 5 ~ "5. Finished high school (Secondary, general (year 4), Secondary school leaving certificate) [Primary]",
            Q163PHI == 6 ~ "6. Incomplete post-secondary technical vocational program [Primary]",
            Q163PHI == 7 ~ "7. Finished post-secondary technical vocational programs (Certificate of Proficiency) [Primary]",
            Q163PHI == 8 ~ "8. Incomplete Bachelor’s degree [Primary]",
            Q163PHI == 9 ~ "9. Bachelor's degree [College]",
            Q163PHI == 10 ~ "10. Master's degree and professional degree in medicine, dentistry, engineering, law [College]",
            Q163PHI == 11 ~ "11. Doctorate Degree [College]",
            Q163PHI %in% c(98, 99) ~ NA_character_,
            #Poland
            Q163POL == 1 ~ "1. Brak formalnego wykszta?cenia lub nieuko?czone podstawowe/No formal education or not completed basic [No education]",
            Q163POL == 2 ~ "2. Podstawowe – 6 klas, lub nieuko?czone gimnazjum/Elementary - 6 classes, or incomplete junior high school [Primary]",
            Q163POL == 3 ~ "3. Gimnazjum lub podstawowe 8 klas/Junior high school or elementary 8 classes [Primary]",
            Q163POL == 4 ~ "4. Niekonczona szkola zawodowa/Incomplete vocational school [Primary]",
            Q163POL == 5 ~ "5. Szkola zawodowa (2-3 lata) LUB nie uko?czona szko?a ?rednia/Vocational school (2-3 years) OR not Completed high school [Primary]",
            Q163POL == 6 ~ "6. Uko?czona szko?a ?rednia (liceum ogólnokszta?c?ce lub technikum)/Completed high school (high school or technical high school) [Primary]",
            Q163POL == 7 ~ "7. Nieuko?czone studia licencjackie/ nieuko?czona szko?a pomaturalna/Unfinished bachelor's degree / unfinished College [Primary]",
            Q163POL == 8 ~ "8. Uko?czone studia licencjackie/in?ynierskie/Completed bachelor / engineering studies [College]",
            Q163POL == 9 ~ "9. Nieuko?czone studia wy?sze (magisterskie)/Unfinished higher education (master's) [College]",
            Q163POL == 10 ~ "10. Uko?czone studia wy?sze (magisterskie)/Completed higher education (master's) [College]",
            Q163POL == 11 ~ "11.  Studia doktoranckie, doktor habilitowany, tytu? profesora/PhD studies, habilitated doctor, title? professor [College]",
            Q163POL %in% c(98, 99) ~ NA_character_,
            #South Russia
            Q163RSA == 1 ~ "1. No formal education [No education]",
            Q163RSA == 2 ~ "2. Incomplete primary school [No education]",
            Q163RSA == 3 ~ "3. Complete primary school [Primary]",
            Q163RSA == 4 ~ "4. National certificate (vocational NC(V)2 - 4); Nated courses level 1-3 (N1- N3) / Other post-primary vocational programs [Primary]",
            Q163RSA == 5 ~ "5. Some high school [Primary]",
            Q163RSA == 6 ~ "6. Matric/Grade 12 [Primary]",
            Q163RSA == 7 ~ "7. Artisan's certificate obtained [Primary]",
            Q163RSA == 8 ~ "8. Professional, technical, secretarial [Primary]",
            Q163RSA == 9 ~ "9. Technikon diploma/degree completed [Primary]",
            Q163RSA == 10 ~ "10. University degree completed [College]",
            Q163RSA %in% c(98, 99) ~ NA_character_,
            #Russia
            Q163RUS == 1 ~ "1. No formal education or less than primary education (completed 1-2 grades only) [No education]",
            Q163RUS == 2 ~ "2. Completed primary general education (completed 3 or 4 grades) [Primary]",
            Q163RUS == 3 ~ "3. Completed 5-9 years of high school, but did not receive a high school diploma [Primary]",
            Q163RUS == 4 ~ "4. Complete general secondary education (diploma after completing 11 (10) grades) [Primary]",
            Q163RUS == 5 ~ "5. Initial vocational education--graduated from vocational school, trade school, factory training, vocational Lyceum (up to [Primary]", #important
            Q163RUS == 6 ~ "6. Initial vocational education--graduated from vocational school, vocational school to obtain a secondary education (1 to [Primary]",
            Q163RUS == 7 ~ "7. Vocational education - graduated from technical school , college (2-4 years) [Primary]",
            Q163RUS == 8 ~ "8. Higher education, 5 to 6 years of training, a Bachelor's degree [College]",
            Q163RUS == 9 ~ "9. Higher education, 5 to 6 years of training, a Masters degree [College]",
            Q163RUS == 10 ~ "10. Post-graduate education (graduate school, residency), Academic degree (PhD) [College]",
            Q163RUS %in% c(98, 99) ~ NA_character_,
            #Senegal
            Q163SEN == 1 ~ "1. Non scolarisé/No formal education (no schooling) [No education]",
            Q163SEN == 2 ~ "2. Éducation pré-scolaire/Pre-primary education [No education]",
            Q163SEN == 3 ~ "3. Enseignement élémentaire (CFEE)/Primary education (CFEE) [Primary]",
            Q163SEN == 4 ~ "4. Enseignement professionnel (CAP)/Vocational education (CAP) [Primary]",
            Q163SEN == 5 ~ "5. Enseignement moyen (1 e cycle) (BFEM)/Lower secondary education/ (BFEM) [Primary]",
            Q163SEN == 6 ~ "6. Enseignement secondaire technique (BEP)/Lower secondary vocational education (BEP) [Primary]",
            Q163SEN == 7 ~ "7. Formation des instituteurs (CEAP)/Teacher training (CEAP) [Primary]",
            Q163SEN == 8 ~ "8. Enseignement secondaire technique (Brevet de technicien)/Upper secondary vocational education (technical baccalaureate) [Primary]",
            Q163SEN == 9 ~ "9. Enseignement secondaire général (2 e cycle) (Baccalauréat)/Upper secondary general education (2 year cycle) (baccalaureate) [Primary]",
            Q163SEN == 10 ~ "10. Enseignement universitaire (Diplôme d'ingénieur; Licence)/University education (Engineering degree; License) [College]",
            Q163SEN == 11 ~ "11. Enseignement universitaire (Master, Maîtrise)/University education (Master’s) [College]",
            Q163SEN == 12 ~ "12. Enseignement supérieur (Doctorate, Master 2, DEA)/Higher education (Doctorate, Master 2, DEA) [College]",
            Q163SEN %in% c(98, 99) ~ NA_character_,
            #South Korea
            Q163SKOR == 1 ~ "1. No formal schooling / Yuchiwon (kindergarten) [No education]",
            Q163SKOR == 2 ~ "2. Incomplete primary education [No education]",
            Q163SKOR == 3 ~ "3. Chodeung-hakgyo (primary school) [Primary]",
            Q163SKOR == 4 ~ "4. Dropped out of middle school [Primary]",
            Q163SKOR == 5 ~ "5. Jung-hakgyo (middle school) [Primary]",
            Q163SKOR == 6 ~ "6. Jeonmungye kodeung-hakgyo (vocational highshool) / Kodeung kisul-hakgyo (trade high school) [Primary]",
            Q163SKOR == 7 ~ "7. Ilbangye kodeung-hakgyo (general high school) [Primary]",
            Q163SKOR == 8 ~ "8. Daehak (junior college, polytechnic college // associate's degree) Semu daehak (college of taxation) or Yukkun samsakwan [Primary]",
            Q163SKOR == 9 ~ "9. Bachelor's degree [College]",
            Q163SKOR == 10 ~ "10. Master's degree [College]",
            Q163SKOR == 11 ~ "11. Doctor's degree [College]",
            Q163SKOR %in% c(98, 99) ~ NA_character_,
            #Spain
            Q163SPA == 1 ~ "1. Sin estudios. No sabe leer ni escribir (analfabeto)/No studies. Cannot read or write (illiterate) [No education]",
            Q163SPA == 2 ~ "2. Menos de 5 años de escolarización/Educación Infantil/Less than 5 years of schooling/Early Childhood Education [No education]",
            Q163SPA == 3 ~ "3. Estudios primarios sin completar/Uncompleted primary studies [No education]",
            Q163SPA == 4 ~ "4. Certificado de Estudios Primarios/Primer Grado (Certificado escolar EGB 1 etapa mas o menos 10 años)/Certificate of Primary Studies / First Grade (EGB school certificate 1 stage more or less 10 years) [Primary]",
            Q163SPA == 5 ~ "5. Segundo grado primer ciclo (Graduado escolar o EGB 2 etapa o 1º o 2º ESO primer ciclo hasta 14 años; Titulo de Graduado)/Second grade first cycle (School graduate or EGB 2 stage or 1st or 2nd ESO first cycle up to 14 years old; graduate title) [Primary]",
            Q163SPA == 6 ~ "6. Formación Profesional de grado medio (certificacion)/Intermediate vocational training (certtification) [Primary]",
            Q163SPA == 7 ~ "7. C9. Segundo grado segundo ciclo (FP I y II Bachiller superior BUP 3º y 4º ESO segundo ciclo COU PREU 1º y 2º Bachillerato ha/Second grade second cycle (FP I and II Upper Baccalaureate BUP 3rd and 4th ESO second cycle COU PREU 1st and 2nd Baccalaureate has [Primary]",
            Q163SPA == 8 ~ "8. Ciclos formativos de grado superior (tecnico superior)/Higher level training cycles (higher technical) [Primary]",
            Q163SPA == 9 ~ "9. Diplomatura universitaria (diplomado universitario, arquitecto tecnico e ingeniero tecnico)/University diploma (university diploma, technical architect and technical engineer) [Primary]",
            Q163SPA == 10 ~ "10. Licenciatura universitaria (licenciado, arquitecto e ingeniero), grado/University degree (graduate, architect and engineer), degree [College]",
            Q163SPA == 11 ~ "11. Master official/Master's [College]",
            Q163SPA == 12 ~ "12. Titulaciones oficiales de estudios de postgrado (doctorado, especialidades medicas/sanitarias)/Official qualifications of postgraduate studies (doctorate, medical/health specialties) [College]",
            Q163SPA %in% c(98, 99) ~ NA_character_,
            #Tanzania
            Q163TAN == 1 ~ "1. No formal education [No education]",
            Q163TAN == 2 ~ "2. Some Primary school [No education]",
            Q163TAN == 3 ~ "3. Completed primary school (Primary School Leaving Certificate - PSLC) [Primary]",
            Q163TAN == 4 ~ "4. Vocational training Vocational training (Grade B Certificate or other vocational certificate) [Primary]",
            Q163TAN == 5 ~ "5. Some secondary education [Primary]",
            Q163TAN == 6 ~ "6. Completed secondary education (Certificate of Secondary Education (CSE) [Primary]",
            Q163TAN == 7 ~ "7. Teacher training, Nursing, Secretarial courses (Diploma / Certificate) [Primary]",
            Q163TAN == 8 ~ "8. Upper secondary (A-Level: Advanced Certificate of Secondary Education (ACSE)) [Primary]",
            Q163TAN == 9 ~ "9. Bachelor [College]",
            Q163TAN == 10 ~ "10. Masters [College]",
            Q163TAN == 11 ~ "11. PhD [College]",
            Q163TAN %in% c(98, 99) ~ NA_character_,
            #Turkey
            Q163TUR == 1 ~ "1. No formal education [No education]",
            Q163TUR == 2 ~ "2. Some primary school [No education]",
            Q163TUR == 3 ~ "3. Completed primary school (Primary School Leaving Certificate - PSLC) [Primary]",
            Q163TUR == 4 ~ "4. Vocational training Vocational training (Grade B Certificate or other vocational certificate) [Primary]",
            Q163TUR == 5 ~ "5. Some secondary education [Primary]",
            Q163TUR == 6 ~ "6. Completed secondary education (Certificate of Secondary Education (CSE)) [Primary]",
            Q163TUR == 7 ~ "7. Teacher training, Nursing, Secretarial courses (Diploma / Certificate) [Primary]",
            Q163TUR == 8 ~ "8. Upper secondary (A-Level: Advanced Certificate of Secondary Education (ACSE)) [Primary]",
            Q163TUR == 9 ~ "9. Bachelor [College]",
            Q163TUR == 10 ~ "10. Master (completed 2 more years after university) [College]",
            Q163TUR == 11 ~ "11. PhD (completed 4-6 more years after university) [College]",
            Q163TUR %in% c(98, 99) ~ NA_character_,
            #Uganda
            Q163UGA == 1 ~ "1. No education at all [No education]",
            Q163UGA == 2 ~ "2. Early childhood education [No education]",
            Q163UGA == 3 ~ "3. Some primary (P1-P6) [No education]",
            Q163UGA == 4 ~ "4. Complete primary (Completed P7 / PLE) [Primary]",
            Q163UGA == 5 ~ "5. Post-primary, non-lower secondary [Primary]",
            Q163UGA == 6 ~ "6. Some secondary (S1-S5) / Lower secondary - O' level [Primary]",
            Q163UGA == 7 ~ "7. Completed secondary / Upper secondary - A' level [Primary]",
            Q163UGA == 8 ~ "8. College (Diploma) [Primary]",
            Q163UGA == 9 ~ "9. Tertiary / Health college (Diploma in Health) [Primary]",
            Q163UGA == 10 ~ "10. Tertiary or post-graduate education (Bachelor, Diploma) [College]",
            Q163UGA == 11 ~ "11. Tertiary education (Master) [College]",
            Q163UGA == 12 ~ "12. Post University ( Doctoral studies / Doctor) [College]",
            Q163UGA %in% c(98, 99) ~ NA_character_,
            #Ukraine
            Q163UKR == 1 ~ "1. Incomplete primary education (less than 4 grades) [No education]",
            Q163UKR == 2 ~ "2. Primary education (grades 4-7) [Primary]",
            Q163UKR == 3 ~ "3. Lower secondary education (grades 8-9) [Primary]",
            Q163UKR == 4 ~ "4. Upper secondary education (grades 10-11) [Primary]",
            Q163UKR == 5 ~ "5. Complete vocational secondary school [Primary]",
            Q163UKR == 6 ~ "6. CPost-secondary professional training other than formal vocational education (PTU) – professional courses (accounting, co [Primary]",
            Q163UKR == 7 ~ "7. Vocational based on full secondary education [Primary]",
            Q163UKR == 8 ~ "8. Incomplete higher education (college) [Primary]",
            Q163UKR == 9 ~ "9. Basic higher education (bachelor) [College]",
            Q163UKR == 10 ~ "10. Complete high education (specialist) [College]",
            Q163UKR == 11 ~ "11. Complete higher education (master) [College]",
            Q163UKR == 12 ~ "12. Postgraduate academic degree, PhD [College]",
            Q163UKR %in% c(98, 99) ~ NA_character_,
            #US
            Q163US == 1 ~ "1. No formal schooling (Preschool or Kindergarten) [No education]",
            Q163US == 2 ~ "2. Less than high school (grades 1 thru 8) [Primary]",
            Q163US == 3 ~ "3. High school incomplete (Grades 9-11 or Grade 12 with NO diploma) [Primary]",
            Q163US == 4 ~ "4. High school graduate (GED or High school diploma) [Primary]",
            Q163US == 5 ~ "5. Vocational Associate's Degree Program [Primary]",
            Q163US == 6 ~ "6. Some college, no degree (includes community college) [Primary]",
            Q163US == 7 ~ "7. Two year associate degree from a college or university [Primary]",
            Q163US == 8 ~ "8. Four year college or university degree/Bachelor’s degree (e.g., BS, BA, AB) [College]",
            Q163US == 9 ~ "9. Postgraduate or professional degree, including master’s degree (e.g., MA, MS, PhD, MD, JD, graduate school) [College]",
            Q163US %in% c(98, 99) ~ NA_character_,
            #Venezuela
            Q163VEN == 1 ~ "1. No tiene educaci¢n formal/Without formal education [No education]",
            Q163VEN == 2 ~ "2. Primaria incompleta/Elementary education incomplete [No education]",
            Q163VEN == 3 ~ "3. Primaria completa/Elementary education complete [Primary]",
            Q163VEN == 4 ~ "4. Secundaria incompleta/Secondary education incomplete [Primary]",
            Q163VEN == 5 ~ "5. Secundaria completa/Secondary education complete [Primary]",
            Q163VEN == 6 ~ "6. Middle / Incomplete high school/Highschool incomplete [Primary]",
            Q163VEN == 7 ~ "7. Media / Bachillerato completo, bachiller/Highschool complete [Primary]",
            Q163VEN == 8 ~ "8. Tecnica incompleta/Technical incomplete [Primary]",
            Q163VEN == 9 ~ "9. Tecnica completa/Technical complete [Primary]",
            Q163VEN == 10 ~ "10. Universitaria incompleta/University incomplete [Primary]",
            Q163VEN == 11 ~ "11. Universitaria completa, licenciatura/University complete, licensed [College]",
            Q163VEN == 12 ~ "12. Postgrado incompleto/Postgraduate incomplete [College]",
            Q163VEN == 13 ~ "13. Postgrado completo, maestria o diplomado/Master Complete [College]",
            Q163VEN == 14 ~ "14. Postgrado completo, doctorado/Doctorate complete [College]",
            Q163VEN %in% c(98, 99) ~ NA_character_,
            #Vietnam
            Q163VIE == 1 ~ "1. No formal schooling [No education]",
            Q163VIE == 2 ~ "2. Incomplete primary [No education]",
            Q163VIE == 3 ~ "3. Primary [Primary]",
            Q163VIE == 4 ~ "4. Lower secondary education (Lower secondary education certificate) [Primary]",
            Q163VIE == 5 ~ "5. Upper secondary, vocational (Vocational education certificate) [Primary]",
            Q163VIE == 6 ~ "6. Upper secondary, general (Upper secondary education certificate) [Primary]",
            Q163VIE == 7 ~ "7. Post-secondary non-tertiary (Post-secondary certificate) [Primary]",
            Q163VIE == 8 ~ "8. Diploma in vocational education [Primary]",
            Q163VIE == 9 ~ "9. College diploma (in general education) [Primary]",
            Q163VIE == 10 ~ "10. Bachelors degree [College]",
            Q163VIE == 11 ~ "11. Masters degree, Graduate diploma [College]",
            Q163VIE == 12 ~ "12. Doctors degree (Ph.D.) [College]",
            Q163VIE %in% c(98, 99) ~ NA_character_ ),    
      
      # respondent's gender (numeric: female = 1; male = 0; other = NA)
        resp_female =
          case_when(
            Q145 == 1 ~ 0,
            Q145 == 2 ~ 1),
      
      # respondent resident in rural (vs urban) area (numeric: rural = 1; urban/semi-urban/peri-urban = 0)
        resp_rural = 
          case_when(
            
          ######## RURAL #######
            # Ethiopia  
              Q220ETH == 2|
            # South Korea
              Q216SKOR == 3|
            # Spain
              Q216SPA == 3|
            # Russia
              Q220RUS == 2|
            # Palestine
              Q219PAL == 2|
            # Uganda
              Q220UGA == 2|
            # France
              Q216FRA == 1| #confirmed
            # Mexico
              Q219MEX == 2|
            # Kenya
              Q219KEN == 2|
            # Australia
              Q214AUS == 2| 
            # Chile 
              Q217CHL == 2| 
            # Vietnam
              Q218VIE == 2|
            # US
              USR == "R"|
            # Germany
              Q216GER == 2|
            # Japan
              Q217JPN == 2| #Peripheral city, town or village
            # Italy
              Q218ITA == 2|
            # Poland 
              Q219POL == 2|
            # Turkey 
              Q219TUR == 2|
            # South Africa
              Q216RSA == 3|
            # Pakistan
              Q220PAK == 2|
            # UK
              Q214BRI == 4|
            # Jordan
              Q218JOR == 2|
            # Philippines
              Q220PHI == 2|
            # Indonesia
              Q219INDO == 2|
            # Peru
              Q219PER == 3|
            # Venezuela
              Q217VEN == 2|
            # Brazil 
              Q219BRA == 2|
            # Ghana
              Q218GHA == 2|
            # Ukraine 
              Q219UKR == 2|
            # Malaysia
              Q219MAL == 2|
            # Argentina
              Q220ARG == 2|
            # Lebanon
              Q217LEB == 2|
            # Tanzania
              Q217TAN == 2|
            # Canada 
              Q216CAN == 2|
            # Nigeria
              Q218NIG == 3|
            # Israel 
              Q217ISR == 2|
            # Senegal
              Q215SEN == 2|
            # China  
              Q220CHI == 2|
            # Burkina Faso
              Q215BUR == 2| 
            # India
              Q223INDIA == 2 ~ 1,
          
          ######## URBAN #######
            # Ethiopia
              Q220ETH == 1|
            # South Korea
              Q216SKOR %in% c(1,2)|
            # Spain 
              Q216SPA %in% c(1,2)|
            # Russia
              Q220RUS == 1|
            # Palestine
              Q219PAL %in% c(1,3)|
            # Uganda
              Q220UGA == 1|
            # France
              Q216FRA %in% c(2:5)|
            # Mexico 
              Q219MEX %in% c(1,3)|
            # Kenya 
              Q219KEN == 1|
            # Australia
              Q214AUS == 1|
            # Chile 
              Q217CHL == 1|
            # Vietnam
              Q218VIE == 1|
            # US 
              USR %in% c("S", "U")|
            # Germany
              Q216GER == 1|
            # Japan
              Q217JPN == 1|
            # Italy
              Q218ITA == 1|
            # Poland
              Q219POL == 1|
            # Turkey
              Q219TUR == 1|
            # South Africa
              Q216RSA %in% c(1,2)|
            # Pakistan
              Q220PAK == 1|
            # UK 
              Q214BRI %in% c(1:3)|
            # Jordan
              Q218JOR == 1|
            # Philippines
              Q220PHI == 1|
            # Indonesia
              Q219INDO == 1|
            # Peru
              Q219PER %in% c(1,2)|
            # Venezuela
              Q217VEN == 1|
            # Brazil  
              Q219BRA == 1|
            # Ghana
              Q218GHA == 1|
            # Ukraine
              Q219UKR == 1|
            # Malaysia
              Q219MAL == 1|
            # Argentina
              Q220ARG == 1|
            # Lebanon
              Q217LEB == 1|
            # Tanzania
              Q217TAN == 1|
            # Canada
              Q216CAN == 1|
            # Nigeria
              Q218NIG %in% c(1,2)|
            # Israel
              Q217ISR == 1|
            # Senegal
              Q215SEN == 1|
            # China 
              Q220CHI == 1|
            # Burkina Faso
              Q215BUR == 1|
            # India
              Q223INDIA == 1  ~ 0,
          
              TRUE ~ NA_real_),
      
    #########################  
    ### SOCIAL DISTANCE 1 ###  
    #########################
    
      # original question number; question text; response options (input above)
        resp_soc_dist_1_qinfo = "NUM: Q45 [Q45A]; QTEXT: I'd like you to rate some different groups of people in (survey country) according to how you feel about them. For each group, please tell me whether your opinion is very favorable, mostly favorable, mostly unfavorable or very unfavorable. Jews.; ROPTIONS: 1 = Very favorable [=0] + 2 = Mostly favorable [=0] + 3 = Mostly unfavorable [=1] + 4 = Very unfavorable [=1]; TARGET: Jewish; TYPE: Favorability",
      
      # original response (as character vector)
        resp_soc_dist_1_original = 
          dplyr::recode(
            as.character(Q45A),
            "8" = NA_character_,
            "9" = NA_character_),       

      # binary recode (numeric: 1 = any negative attitude expressed; 0 otherwise)
        resp_soc_dist_1_bin_recode = 
          case_when(
            Q45A %in% c(1:2) ~ 0,
            Q45A %in% c(3:4) ~ 1,
            TRUE ~ NA_real_),
    
    #########################  
    ### SOCIAL DISTANCE 2 ###  
    #########################
    
      # original question number; question text; response options (input above)
        resp_soc_dist_2_qinfo = "NUM: Q45 [Q45C]; QTEXT: I'd like you to rate some different groups of people in (survey country) according to how you feel about them. For each group, please tell me whether your opinion is very favorable, mostly favorable, mostly unfavorable or very unfavorable. Muslims.; ROPTIONS: 1 = Very favorable [=0] + 2 = Mostly favorable [=0] + 3 = Mostly unfavorable [=1] + 4 = Very unfavorable [=1]; TARGET: Muslim; TYPE: Favorability",
      
      # original response (as character vector)
        resp_soc_dist_2_original = 
          dplyr::recode(
            as.character(Q45C),
            "8" = NA_character_,
            "9" = NA_character_),       

      # binary recode (numeric: 1 = any negative attitude expressed; 0 otherwise)
        resp_soc_dist_2_bin_recode = 
          case_when(
            Q45C %in% c(1:2) ~ 0,
            Q45C %in% c(3:4) ~ 1,
            TRUE ~ NA_real_),
    
    ############################  
    ### GENERAL SOCIAL TRUST ###  
    ############################
    
      # original question number; question text; response options (input above)
        resp_gentrust_qinfo = NA, # checked; no question asked

      # original response (as character vector)
        resp_gentrust_original = NA,       

      # binary recode (numeric: 1 = any negative attitude expressed; 0 otherwise)
        resp_gentrust_bin_recode = NA,    
    
    #########################  
    ##### RELIGIOSITY #######  
    #########################
    
      # original question number; question text; response options (input above)
        resp_religiosity_qinfo = "NUM: Q152; QTEXT: How important is religion in your life – very important, somewhat important, not too important, or not at all important?; ROPTIONS: 1 = Very important + 2 = Somewhat important + 3 = Not too important + 4 = Not at all important",
  
      # original response (as numeric vector, with non-substantive responses coded as NA_real_)
        resp_religiosity_original = 
          dplyr::recode(
            as.numeric(Q152),
            `8` = NA_real_,
            `9` = NA_real_),       

      # recode (numeric: scaled 0-1, where 1 is more religious)
        resp_religiosity_recode = (4 - resp_religiosity_original)/3
    
    ) %>% 
    select(starts_with("resp_"))
```

# Clean Pew Global Attitudes Spring 2016

```{r}
# declare religion variables
  rel_vars <- 
    c("Q109AUS", "Q109CAN", "Q109CHI", "Q109IND", 
      "Q109FRA", "Q109GER", "Q109GRE", "Q109HUN", 
      "Q109ITA", "Q109NET", "Q109SPA", "Q109POL",
      "Q109SWE", "Q109BRI", "Q109JPN", "Q109KEN", 
      "Q109NIG", "Q109RSA", "Q109USA")

# clean pew global attitudes spring 2016
  n11_clean_pew_gap_spring_16 <-
    pew_gap_spring_16_raw %>% 
    mutate(
      
    #########################  
    ####### META-DATA #######  
    #########################      
      
      # source name (character vector, title case)
        resp_source = "Pew Global Attitudes Project",
        
      # round number (character vector, title case)  
        resp_round = "Spring 2016",
      
      # url to dataset source, where publicly available (character vector)
        resp_original_data_url = "pewrsr.ch/3wToJeP",

      # survey mode (in-person/phone/internet/mail)
        resp_survey_mode = 
          case_when(
            country %in% c(1, 2, 4, 5, 10, 12, 16, 18, 19, 17) ~ "phone",
            TRUE ~ "in-person"),

      # country (character vector; list of countries as written in original source)
        resp_country_original = to_character(country),

      # country (character vector; converts to countrycode county.name list)
        resp_country_common = 
          countryname(resp_country_original),
        
      # interview date (variable of class Date; if only month given, input 1st of month)
        resp_interview_date = as.Date(qdate),
   
    #########################  
    ##### DEMOGRAPHICS ######  
    #########################
        
      # respondent denomination (recoded jointly below)
        Q109AUSb = to_character(Q109AUSb),
        Q109CANb = to_character(Q109CANb),
        Q109FRAb = to_character(Q109FRAb),
        Q109GERb = to_character(Q109GERb),
        Q109GREb = to_character(Q109GREb),
        Q109HUNb = to_character(Q109HUNb),
        Q109ITAb = to_character(Q109ITAb),
        Q109NETb = to_character(Q109NETb),
        Q109SPAb = to_character(Q109SPAb),
        Q109POLb = to_character(Q109POLb),
        Q109SWEb = to_character(Q109SWEb),
        Q109BRIb = to_character(Q109BRIb),
        Q109JPNB = to_character(Q109JPNB),
        Q109KENb = to_character(Q109KENb),
        Q109NIGb = to_character(Q109NIGb),
        Q109RSAb = to_character(Q109RSAb),
        Q109USA = q109c, #usa, different
        Q109USAb = to_character(q109d), #usa, different
        across(rel_vars, to_character)) %>% 
      mutate(
        Q109AUS = case_when(Q109AUSb == "Yes would" ~ "Christian", TRUE ~ Q109AUS),
        Q109CAN = case_when(Q109CANb == "Yes, Christian" ~ "Christian", TRUE ~ Q109CAN),
        Q109FRA = case_when(Q109FRAb == "Yes, Christian" ~ "Christian", TRUE ~ Q109FRA),
        Q109GER = case_when(Q109GERb == "Yes, Christian" ~ "Christian", TRUE ~ Q109GER),
        Q109GRE = case_when(Q109GREb == "Yes, Christian" ~ "Christian", TRUE ~ Q109GRE),
        Q109HUN = case_when(Q109HUNb == "Yes, Christian" ~ "Christian", TRUE ~ Q109HUN),
        Q109ITA = case_when(Q109ITAb == "Yes, Christian" ~ "Christian", TRUE ~ Q109ITA),
        Q109NET = 
          case_when(
            Q109NETb == "Christian" ~ "Christian", 
            Q109NETb == "Muslim" ~ "Muslim", 
            TRUE ~ Q109NET),
        Q109SPA = case_when(Q109SPAb == "Yes, Christian" ~ "Christian", TRUE ~ Q109SPA),
        Q109POL = case_when(Q109POLb == "Yes, Christian" ~ "Christian", TRUE ~ Q109POL),
        Q109SWE = 
          case_when(
            Q109SWEb == "Christian" ~ "Christian", 
            Q109SWEb == "Muslim" ~ "Muslim", 
            TRUE ~ Q109SWE), 
        Q109BRI = case_when(Q109BRIb == "Yes, Christian" ~ "Christian", TRUE ~ Q109BRI),
        Q109JPN = case_when(Q109JPNB == "Yes, Buddhist" ~ "Buddhist", TRUE ~ Q109JPN),
        Q109KEN = case_when(Q109KENb == "Christian" ~ "Christian", TRUE ~ Q109KEN),
        Q109NIG = 
          case_when(
            Q109NIGb == "Christian" ~ "Christian", 
            Q109NIGb == "Muslim" ~ "Muslim", 
            TRUE ~ Q109NIG),
        Q109RSA = case_when(Q109RSAb == "Yes" ~ "Christian", TRUE ~ Q109RSA),
        Q109USA = case_when(Q109USAb == "Yes" ~ "Christian", TRUE ~ Q109USA),
        resp_denomination = coalesce(!!! select(., c(rel_vars))), 

      # respondent's age (character vector; bins denoted by single dash ["18-25"])
        resp_age =
          dplyr::recode(
            as.character(q108),
            "97" = "97+",
            "98" = NA_character_,
            "99" = NA_character_),        
      
      # respondent's education level
        resp_education_original =
          case_when(
            #Australia
            Q113AUSa == 1 ~ "1. Pre-Primary school or less (Preschool or Kindergarten) [No education]",
            Q113AUSa == 2 ~ "2. Primary school [Primary]",
            Q113AUSa == 3 ~ "3. Some secondary/ technical [Primary]",
            Q113AUSa == 4 ~ "4. School certificate/ intermediate/ 4th form/ year 10/ junior/ achievement certificate (junior/lower secondary school) [Primary]",
            Q113AUSa == 5 ~ "5. 5th form/ year 11/ leaving/ sub-senior certificate [Primary]",
            Q113AUSa == 6 ~ "6. Finished matriculation/ higher school certificate (HSC) or (VCE)/ year 12/ senior certificate (senior/upper secondary sc [Primary]",
            Q113AUSa == 7 ~ "7. Some TAFE/now at TAFE [Primary]",
            Q113AUSa == 8 ~ "8. Finished technical school/ TAFE/ other certificate or apprenticeship [Primary]",
            Q113AUSa == 9 ~ "9. Some university/ now at university [Primary]",
            Q113AUSa == 10 ~ "10. Diploma from college of advanced education/ TAFE/ management training including diploma [Primary]",
            Q113AUSa == 11 ~ "11. Degree from university/ college of advanced education [College]",
            Q113AUSa == 12 ~ "12. Graduate Diploma / Masters [College]",
            Q113AUSa == 13 ~ "13. Ph.D [College]",
            Q113AUSa %in% c(98, 99) ~ NA_character_,
            #Canada
            Q113CANa == 1 ~ "1. No formal schooling (Preschool or Kindergarten) [No education]",
            Q113CANa == 2 ~ "2. Completed elementary school (Grade 6) [Primary]",
            Q113CANa == 3 ~ "3. Some junior high school (Grades 7) [Primary]",
            Q113CANa == 4 ~ "4. Completed junior high school (Grade 8) [Primary]",
            Q113CANa == 5 ~ "5. Some senior high school or vocational secondary school (Grade 9-10 in Quebec/Grade 9-11 in the rest of Canada) [Primary]",
            Q113CANa == 6 ~ "6. Completed secondary school or vocational secondary school (secondary school diploma) (Grade 11 in Quebec/Grade 12 in res [Primary]",
            Q113CANa == 7 ~ "7. College preliminary year/Trade certificate/upgrading certificate [Primary]",
            Q113CANa == 8 ~ "8. Undergraduate qualifying program/University Transfer Program (CEGEP in Quebec) [Primary]",
            Q113CANa == 9 ~ "9. Technical diploma/community college diploma or certificate [Primary]",
            Q113CANa == 10 ~ "10. Four year college or university degree/Bachelor’s degree (e.g., BS, BA, AB) [College]",
            Q113CANa == 11 ~ "11. Postgraduate or professional degree, including master’s degree (e.g., MA, MS, PhD, MD, DESS, graduate school) [College]",
            Q113CANa %in% c(98, 99) ~ NA_character_,
            #China
            Q113CHIa == 1 ~ "1. No education [No education]",
            Q113CHIa == 2 ~ "2. Incomplete primary education [No education]",
            Q113CHIa == 3 ~ "3. Primary school [Primary]",
            Q113CHIa == 4 ~ "4. Junior secondary school [Primary]",
            Q113CHIa == 5 ~ "5. Secondary Vocational School/Technical secondary school [Primary]",
            Q113CHIa == 6 ~ "6. Senior High School [Primary]",
            Q113CHIa == 7 ~ "7. Higher technical & vocational School/Junior College [Primary]",
            Q113CHIa == 8 ~ "8. Bachelor (Undergraduate) [College]",
            Q113CHIa == 9 ~ "9. Master [College]",
            Q113CHIa == 10 ~ "10. Doctorate [College]",
            Q113CHIa %in% c(98, 99) ~ NA_character_,
            #India
            Q113INDa == 1 ~ "1. Illiterate [No education]",
            Q113INDa == 2 ~ "2. Literate but no formal schooling [No education]",
            Q113INDa == 3 ~ "3. Primary Education (primary certificate) [Primary]",
            Q113INDa == 4 ~ "4. Upper Primary (Upper Primary Certificate) [Primary]",
            Q113INDa == 5 ~ "5. High School; Industrial Training Institute (matriculation certificate or ITI certificate [Primary]",
            Q113INDa == 6 ~ "6. Senior Secondary / intermediate (Senior Secondary School Leaving Certificate) [Primary]",
            Q113INDa == 7 ~ "7. Technical Education Training; Junior teachers training (diploma) [Primary]",
            Q113INDa == 8 ~ "8. Tertiary, technical higher education (diploma / bachelor) [Primary]",
            Q113INDa == 9 ~ "9. Bachelor (university 1st) [College]",
            Q113INDa == 10 ~ "10. Master (university  2nd) [College]",
            Q113INDa == 11 ~ "11. Doctor (Tertiary 2nd) [College]",
            Q113INDa %in% c(98, 99) ~ NA_character_,
            #France
            Q113FRAa == 1 ~ "1. Non scolarisé ou école primaire non achevée/No schooling or primary school not complete [No education]",
            Q113FRAa == 2 ~ "2. Ecole primaire avec le CEP/Primary school with CEP [Primary]",
            Q113FRAa == 3 ~ "3. Enseignement du premier cycle du second degré – Collège (BREVET)/Education of the first grade of the second degree - College (BREVET) [Primary]",
            Q113FRAa == 4 ~ "4. Enseignement de second cycle professionnel du second degré (sous statut scolaire et apprentissage) (C.A.P, B.E.P, etc)/Education of the second grade of the second degree (under school status and apprenticeship) (C.A.P, B.E.P, etc) [Primary]",
            Q113FRAa == 5 ~ "5. Enseignement de second cycle général du second degré (Baccalauréat général et technologique)/General secondary education of the second degree (General and technological baccalaureate) [Primary]",
            Q113FRAa == 6 ~ "6. Enseignement des écoles sociales et autres formations (Diplôme de moniteur éducateur, éducateur technique spécialisé)/Education of social schools and other training (Diploma of educator monitor, specialized technical educator) [Primary]",
            Q113FRAa == 7 ~ "7. Préparation à l'entrée d'une formation sélective de niveau 5B (Prépa entrée écoles paramédicales et sociales, classes de)/Preparation for entry into selective level 5B training (Preparation for entry to paramedical and social schools) [Primary]",
            Q113FRAa == 8 ~ "8. Enseignement pré-universitaire (Diplôme de la capacité en droit, diplôme d'accès aux études universitaires)/Pre-university education (Diploma of capacity in law, diploma of access to university studies) [Primary]",
            Q113FRAa == 9 ~ "9. Diplôme universitaire de technologie (DUT) // Brevet de technicien supérieur (BTS), Diplôme des métiers d'art (DMA)/University diploma in technology (DUT) // Higher technician certificate (BTS), Diploma in crafts (DMA) [Primary]",
            Q113FRAa == 10 ~ "10. Enseignement de premier cycle des études universitaires (Diplôme d'études universitaires générales (DEUG), LICENCE, LICE/Undergraduate education of university studies (Diploma of general university studies (DEUG), LICENCE, LICE [College]",
            Q113FRAa == 11 ~ "11. Enseignement de deuxième cycle des études universitaires (MAITRISE, Diplome (DESS), Master (LMD), Diplome Ingenieur)/Second-degree education of university studies (MASTER'S, Diplome (DESS), Master's (LMD), Diplome Ingenieur) [College]",
            Q113FRAa == 12 ~ "12. Enseignement de troisième cycle des études universitaires  (Diplôme d'études approfondies (DEA), Diplome de docteur)/Postgraduate education of university studies (Diploma of advanced studies (DEA), Doctor's degree) [College]",
            Q113FRAa %in% c(98, 99) ~ NA_character_,
            #Germany
            Q113GERa == 1 ~ "1. kein Schulabschluss / Grundschule/no school certificate / elementary school [Primary]",
            Q113GERa == 2 ~ "2. Volks- oder Hauptschule/Elementary or secondary school [Primary]",
            Q113GERa == 3 ~ "3. Weiterführende Schule ohne Abitur/Secondary school with Abitur [Primary]",
            Q113GERa == 4 ~ "4. Abitur, Fachhochschulreife, Hochschulreife/Abitur, technical college entrance qualification, university entrance qualification [Primary]",
            Q113GERa == 5 ~ "5. abgeschlossenes Bachelor-Studium/ Grundstudium/ Vordiplom/ Berufsakademie, nicht abgeschl. Haupt- bzw. Master-Studium/Completed bachelor’s degree/ basic studies/ intermediate diploma/ professional academy, not completed Main or Master's degree [College]",
            Q113GERa == 6 ~ "6. abgeschlossenes Haupt- bzw. Master-Studium/Completed main or Master's degree [College]",
            Q113GERa == 7 ~ "7. Promotion, Doktorat/Promotion, Doctorate [College]",
            Q113GERa %in% c(98, 99) ~ NA_character_,
            #Greece
            Q113GREa == 1 ~ "1. No formal schooling (Preschool or Kindergarten) [No education]",
            Q113GREa == 2 ~ "2. Baccalaureate Primary [Primary]",
            Q113GREa == 3 ~ "3. 3rd Certificate of Secondary Education (high school) [Primary]",
            Q113GREa == 4 ~ "4. Technical Vocational Educational Schools (upper secondary education) [Primary]",
            Q113GREa == 5 ~ "5. Certificate of Secondary Education (high school) [Primary]",
            Q113GREa == 6 ~ "6. Vocational Training Institutes (IEK) [Primary]",
            Q113GREa == 7 ~ "7. Technical Educational Institutes (T.E.I.) [Primary]",
            Q113GREa == 8 ~ "8. Technological Educational Institute (A.TEI), Schools of Higher Vocational Education, (after the anotatopoiisi) [Primary]",
            Q113GREa == 9 ~ "9. University, Hellenic Open University (EAP) (Higher tertiary eduation, BA) [College]",
            Q113GREa == 10 ~ "10. University, Polytechnic or Hellenic Open University (Postgraduate diploma / Master) [College]",
            Q113GREa == 11 ~ "11. University, Polytechnic or Hellenic Open University (EAP) (PhD) [College]",
            Q113GREa %in% c(98, 99) ~ NA_character_,
            #Hungary
            Q113HUNa == 1 ~ "1. nem járt iskolába/No formal education [No education]",
            Q113HUNa == 2 ~ "2. 1-7 osztály elemi/általános iskola vagy azzal egyenértéku/Grades 1-7 elementary / primary school or equivalent [No education]",
            Q113HUNa == 3 ~ "3. befejezett általános iskola vagy azzal egyenértéku/completed primary school or equivalent [Primary]",
            Q113HUNa == 4 ~ "4. szakmunkásképzo, szakiskola/vocational training, vocational school [Primary]",
            Q113HUNa == 5 ~ "5. érettségi, befejezett szakközépiskola/graduation, completed vocational high school [Primary]",
            Q113HUNa == 6 ~ "6. érettségi, befejezett gimnázium/graduation, Completed high school [Primary]",
            Q113HUNa == 7 ~ "7. érettségire épülo, felsofokra nem akkreditált szakképzés;/non-tertiary vocational training based on high school diploma [Primary]",
            Q113HUNa == 8 ~ "8. felsofokra akkreditált szakképzés, felsofokú technikum/tertiary-accredited vocational training, tertiary technical school [Primary]",
            Q113HUNa == 9 ~ "9. foiskolai diploma (bachelor’s degree) [College]",
            Q113HUNa == 10 ~ "10. egyetemi diploma (master’s degree) [College]",
            Q113HUNa == 11 ~ "11. posztgraduális végzettség/ felsofokú végzettség, tudományos/postgraduate / tertiary degree, academic [College]",
            Q113HUNa %in% c(98, 99) ~ NA_character_,
            #Italy
            Q113ITAa == 1 ~ "1. Senza titoli di studi/Without educational qualifications [No education]",
            Q113ITAa == 2 ~ "2. Scuola primaria (scuola elementare)/Primary school (elementary school) [Primary]",
            Q113ITAa == 3 ~ "3. Scuola secondaria di primo grado (scuola media)/Lower secondary school (secondary school) [Primary]",
            Q113ITAa == 4 ~ "4. Scuola secondaria di secondo grado (scuola superiore)/Second grade secondary school (high school) [Primary]",
            Q113ITAa == 5 ~ "5. Specializzazione professionale post-diploma (istruzione professionale successiva al diploma di scuola superiore)/Post-diploma professional specialization (vocational education after high school graduation) [Primary]",
            Q113ITAa == 6 ~ "6. Laurea triennale/Three-year degree [College]",
            Q113ITAa == 7 ~ "7. Laurea magistrale/Master's degree [College]",
            Q113ITAa == 8 ~ "8. Specializzazione post-laurea/Postgraduate specialization [College]",
            Q113ITAa %in% c(9, 10, 98, 99) ~ NA_character_,
            #Netherlands
            Q113NETa == 1 ~ "1. No formal school (preschool or kindergarten) [No education]",
            Q113NETa == 2 ~ "2. Primary school [Primary]",
            Q113NETa == 3 ~ "3. MBO 1 assistent [Primary]",
            Q113NETa == 4 ~ "4. Senior general secondary (VAVO-HAVO/VWO) [Primary]",
            Q113NETa == 5 ~ "5. Intermediate Vocational Training (MBO 2/3) [Primary]",
            Q113NETa == 6 ~ "6. MBO 4 Specialist (specialist training diploma) [Primary]",
            Q113NETa == 7 ~ "7. HBO-einddiploma, WO-bachelor [College]",
            Q113NETa == 8 ~ "8. WO-doctorandus, WO-master, HBO-vervolgopleiding [College]",
            Q113NETa == 9 ~ "9. Doctorate [College]",
            Q113NETa %in% c(9, 10, 98, 99) ~ NA_character_,
            #Spain
            Q113SPAa == 1 ~ "1. Sin estudios. No sabe leer ni escribir (analfabeto)/No studies. Cannot read or write (illiterate) [No education]",
            Q113SPAa == 2 ~ "2. Menos de 5 años de escolarización/Educación Infantil/Less than 5 years of schooling/Early Childhood Education [No education]",
            Q113SPAa == 3 ~ "3. Estudios primarios sin completar/Uncompleted primary studies [No education]",
            Q113SPAa == 4 ~ "4. Certificado de Estudios Primarios/Primer Grado (Certificado escolar EGB 1 etapa mas o menos 10 años)/Certificate of Primary Studies / First Grade (EGB school certificate 1 stage more or less 10 years) [Primary]",
            Q113SPAa == 5 ~ "5. Segundo grado primer ciclo (Graduado escolar o EGB 2 etapa o 1º o 2º ESO primer ciclo hasta 14 años; Titulo de Graduado)/Second grade first cycle (School graduate or EGB 2 stage or 1st or 2nd ESO first cycle up to 14 years old; graduate title) [Primary]",
            Q113SPAa == 6 ~ "6. Formación Profesional de grado medio (certificacion)/Intermediate vocational training (certtification) [Primary]",
            Q113SPAa == 7 ~ "7. C9. Segundo grado segundo ciclo (FP I y II Bachiller superior BUP 3º y 4º ESO segundo ciclo COU PREU 1º y 2º Bachillerato ha/Second grade second cycle (FP I and II Upper Baccalaureate BUP 3rd and 4th ESO second cycle COU PREU 1st and 2nd Baccalaureate has [Primary]",
            Q113SPAa == 8 ~ "8. Ciclos formativos de grado superior (tecnico superior)/Higher level training cycles (higher technical) [Primary]",
            Q113SPAa == 9 ~ "9. Diplomatura universitaria (diplomado universitario, arquitecto tecnico e ingeniero tecnico)/University diploma (university diploma, technical architect and technical engineer) [Primary]",
            Q113SPAa == 10 ~ "10. Licenciatura universitaria (licenciado, arquitecto e ingeniero), grado/University degree (graduate, architect and engineer), degree [College]",
            Q113SPAa == 11 ~ "11. Master official/Master's [College]",
            Q113SPAa == 12 ~ "12. Titulaciones oficiales de estudios de postgrado (doctorado, especialidades medicas/sanitarias)/Official qualifications of postgraduate studies (doctorate, medical/health specialties) [College]",
            Q113SPAa %in% c(98, 99) ~ NA_character_,
            #Poland
            Q113POLa == 1 ~ "1. Brak formalnego wykszta?cenia lub nieuko?czone podstawowe/No formal education or not completed basic [No education]",
            Q113POLa == 2 ~ "2. Podstawowe – 6 klas, lub nieuko?czone gimnazjum/Elementary - 6 classes, or incomplete junior high school [Primary]",
            Q113POLa == 3 ~ "3. Gimnazjum lub podstawowe 8 klas/Junior high school or elementary 8 classes [Primary]",
            Q113POLa == 4 ~ "4. Niekonczona szkola zawodowa/Incomplete vocational school [Primary]",
            Q113POLa == 5 ~ "5. Szkola zawodowa (2-3 lata) LUB nie uko?czona szko?a ?rednia/Vocational school (2-3 years) OR not Completed high school [Primary]",
            Q113POLa == 6 ~ "6. Uko?czona szko?a ?rednia (liceum ogólnokszta?c?ce lub technikum)/Completed high school (high school or technical high school) [Primary]",
            Q113POLa == 7 ~ "7. Nieukonczone studia licencjackie/ nieuko?czona szko?a pomaturalna/Unfinished bachelor's degree / unfinished College [Primary]",
            Q113POLa == 8 ~ "8. Ukonczone studia licencjackie/in?ynierskie/Completed bachelor / engineering studies [College]",
            Q113POLa == 9 ~ "9. Nieukonczone studia wy?sze (magisterskie)/Unfinished higher education (master's) [College]",
            Q113POLa == 10 ~ "10. Ukonczone studia wy?sze (magisterskie)/Completed higher education (master's) [College]",
            Q113POLa == 11 ~ "11.  Studia doktoranckie, doktor habilitowany, tytu? profesora/PhD studies, habilitated doctor, title? professor [College]",
            Q113POLa %in% c(98, 99) ~ NA_character_,
            #Sweden
            Q113SWEa == 1 ~ "1. No formal school (preschool or kindergarten) [No education]",
            Q113SWEa == 2 ~ "2. Grundskolan (compulsory school) grades 1-6, including adult [Primary]",
            Q113SWEa == 3 ~ "3. Grundskolan (compulsory school) grades 7-9, including [Primary]",
            Q113SWEa == 4 ~ "4. Upper secondary school, including folkhögskolan [Primary]",
            Q113SWEa == 5 ~ "5. National state schools for adults – post-secondary training [Primary]",
            Q113SWEa == 6 ~ "6. Advanced vocational training, 2-3 years [Primary]",
            Q113SWEa == 7 ~ "7. University diploma, general or professional [Primary]",
            Q113SWEa == 8 ~ "8. Bachelor’s degree [College]",
            Q113SWEa == 9 ~ "9. Master’s degree, general or professional [College]",
            Q113SWEa == 10 ~ "10. Licentiate or Doctorate [College]",
            Q113SWEa %in% c(98, 99) ~ NA_character_,
            #Britain
            Q113BRIa == 1 ~ "1. No qualifications obtained [Primary]",
            Q113BRIa == 2 ~ "2. Primary school / infant & junior schools [Primary]",
            Q113BRIa == 3 ~ "3. GCSE /CSE/GCE O-level (any grades); Scottish SCE Ordinary [Primary]",
            Q113BRIa == 4 ~ "4. A-levels; S-levels; A2-level; GCE Applied A-level; NVQ/SVQ [Primary]",
            Q113BRIa == 5 ~ "5. Nursing certificate [Primary]",
            Q113BRIa == 6 ~ "6. Foundation degree/HND/Higher Education Diploma/NVQ 4, NVQ5 [Primary]",
            Q113BRIa == 7 ~ "7. University (Bachelor) [College]",
            Q113BRIa == 8 ~ "8. University (Masters degree) [College]",
            Q113BRIa == 9 ~ "9. University (Doctorate, PHD) [College]",
            Q113BRIa %in% c(98, 99) ~ NA_character_,
            #Japan
            Q113JPNA == 1 ~ "1. No formal schooling (Preschool or Kindergarten) [No education]",
            Q113JPNA == 2 ~ "2. Elementary school (Shogakko) [Primary]",
            Q113JPNA == 3 ~ "3. Lower secundary school (Chuto-kyoiku-gakko (Zenki katei); Chugakko [Primary]",
            Q113JPNA == 4 ~ "4. College of technology [Primary]",
            Q113JPNA == 5 ~ "5. Upper Secondary school [Primary]",
            Q113JPNA == 6 ~ "6. Junior college (associate degree), special training college (diploma = senmonshi or advanced diploma = kodo-senmonshi) [Primary]",
            Q113JPNA == 7 ~ "7. Bachelor's degree (Gakushi) [College]",
            Q113JPNA == 8 ~ "8. Master's degree (Shushi ) or Juris Doctor (Homuhakushi) [College]",
            Q113JPNA == 9 ~ "9. Doctors degree (Hakushi) [College]",
            Q113JPNA %in% c(98, 99) ~ NA_character_,
            #Kenya
            Q113KENa == 1 ~ "1. No formal education [No education]",
            Q113KENa == 2 ~ "2. Early childhood development programme (ECD) [No education]",
            Q113KENa == 3 ~ "3. First stage of primary education (Standards 1 to 6) [Primary]",
            Q113KENa == 4 ~ "4. Second stage of primary education (Standards 7 to 8) (KCPE) [Primary]",
            Q113KENa == 5 ~ "5. Youth polytechnics programme (Craft trade certificate) [Primary]",
            Q113KENa == 6 ~ "6. Incomplete secondary school [Primary]",
            Q113KENa == 7 ~ "7. Secondary education (KCSE) [Primary]",
            Q113KENa == 8 ~ "8. High School (Form 6) A level Certificate [Primary]",
            Q113KENa == 9 ~ "9. Technical and industrial vocational education training (TIVET) [Primary]",
            Q113KENa == 10 ~ "10. Teacher training; Polytechnics programmes [Primary]",
            Q113KENa == 11 ~ "11. Bachelor [College]",
            Q113KENa == 12 ~ "12. Masters [College]",
            Q113KENa == 13 ~ "13. Doctorate [College]",
            Q113KENa %in% c(98, 99) ~ NA_character_,
            #Nigeria
            Q113NIGa == 1 ~ "1. No formal education [No education]",
            Q113NIGa == 2 ~ "2 Pre-primary education [No education]",
            Q113NIGa == 3 ~ "3. Primary education [Primary]",
            Q113NIGa == 4 ~ "4. Junior secondary [Primary]",
            Q113NIGa == 5 ~ "5. Senior secondary [Primary]",
            Q113NIGa == 6 ~ "6. Secondary technical schools programmes [Primary]",
            Q113NIGa == 7 ~ "7.  National Diploma, NCE [Primary]",
            Q113NIGa == 8 ~ "8. Higher National Diploma [Primary]",
            Q113NIGa == 9 ~ "9. Bachelor [College]",
            Q113NIGa == 10 ~ "10. Master [College]",
            Q113NIGa == 11 ~ "11. PhD [College]",
            Q113NIGa %in% c(98, 99) ~ NA_character_,
            #South Russia
            Q113RSAa == 1 ~ "1. No formal education [No education]",
            Q113RSAa == 2 ~ "2. Incomplete primary school [No education]",
            Q113RSAa == 3 ~ "3. Complete primary school [Primary]",
            Q113RSAa == 4 ~ "4. National certificate (vocational NC(V)2 - 4); Nated courses level 1-3 (N1- N3) / Other post-primary vocational programs [Primary]",
            Q113RSAa == 5 ~ "5. Some high school [Primary]",
            Q113RSAa == 6 ~ "6. Matric/Grade 12 [Primary]",
            Q113RSAa == 7 ~ "7. Artisan's certificate obtained [Primary]",
            Q113RSAa == 8 ~ "8. Professional, technical, secretarial [Primary]",
            Q113RSAa == 9 ~ "9. Technikon diploma/degree completed [Primary]",
            Q113RSAa == 10 ~ "10. University degree completed [College]",
            Q113RSAa %in% c(98, 99) ~ NA_character_,
            #US
            q113USAa == 1 ~ "1. No formal schooling (Preschool or Kindergarten) [No education]",
            q113USAa == 2 ~ "2. Less than high school (grades 1 thru 8) [Primary]",
            q113USAa == 3 ~ "3. High school incomplete (Grades 9-11 or Grade 12 with NO diploma) [Primary]",
            q113USAa == 4 ~ "4. High school graduate (GED or High school diploma) [Primary]",
            q113USAa == 5 ~ "5.  Vocational Associate's Degree Program [Primary]",
            q113USAa == 6 ~ "6. Some college, no degree (includes community college) [Primary]",
            q113USAa == 7 ~ "7. Two year associate degree from a college or university [Primary]",
            q113USAa == 8 ~ "8. Four year college or university degree/Bachelor’s degree (e.g., BS, BA, AB) [College]",
            q113USAa == 9 ~ "9. Postgraduate or professional degree, including master’s degree (e.g., MA, MS, PhD, MD, JD, graduate school) [College]",
            q113USAa %in% c(98, 99) ~ NA_character_),       
      
      # respondent's gender (numeric: female = 1; male = 0; other = NA)
        resp_female =
          case_when(
            q107 == 1 ~ 0,
            q107 == 2 ~ 1),
      
      # respondent resident in rural (vs urban) area (numeric: rural = 1; urban/semi-urban/peri-urban = 0)
        resp_rural = # urbanity variable is missing for many countries
          case_when(
            
          ######## RURAL #######            
            # Australia
              QS8AUS == 2|
            # Canada
              QS8CAN == 2|
            # China
              QS8kCHI == 2|
            # France
              QS6FRA %in% c(3,4)|
            # Germany 
            # Greece
              QS8GRE == 3|
            # Hungary
              QS8HUN == 3|
            # India
              QS8lINDIA == 2|
            # Italy
              QS8ITA == 3|
            # Japan 
            # Kenya
              QS8KEN == 2|
            # Netherlands 
            # Nigeria
              QS8NIG == 2|
            # Poland
              QS8POL == 3|
            # South Africa
              QS6RSA == 2 ~ 1,
            # Spain 
            # Sweden 
            # UK 
            # US  
          
          ######## URBAN #######            
            # Australia
              QS8AUS == 1|
            # Canada
              QS8CAN == 1|
            # China 
              QS8kCHI == 1|
            # France 
              QS6FRA %in% c(1,2,5)|
            # Germany 
            # Greece
              QS8GRE %in% c(1,2)|
            # Hungary
              QS8HUN %in% c(1,2)|
            # India
              QS8lINDIA == 1|
            # Italy 
              QS8ITA %in% c(1,2)|
            # Japan 
            # Kenya
              QS8KEN == 1|
            # Netherlands 
            # Nigeria
              QS8NIG == 1|
            # Poland
              QS8POL %in% c(1,2)|
            # South Africa
              QS6RSA %in% c(3,4) ~ 0,
            # Spain 
            # Sweden 
            # UK 
            # US 
          
              TRUE ~ NA_real_),
      
    #########################  
    ### SOCIAL DISTANCE 1 ###  
    #########################
    
      # original question number; question text; response options (input above)
        resp_soc_dist_1_qinfo = "NUM: Q36 [Q36a]; QTEXT: I'd like you to rate some different groups of people in (survey country) according to how you feel about them. For each group, please tell me whether your opinion is very favorable, mostly favorable, mostly unfavorable or very unfavorable. Jews.; ROPTIONS: 1 = Very favorable [=0] + 2 = Mostly favorable [=0] + 3 = Mostly unfavorable [=1] + 4 = Very unfavorable [=1]; TARGET: Jewish; TYPE: Favorability",
      
      # original response (as character vector)
        resp_soc_dist_1_original = 
          dplyr::recode(
            as.character(Q36a),
            "8" = NA_character_,
            "9" = NA_character_),       

      # binary recode (numeric: 1 = any negative attitude expressed; 0 otherwise)
        resp_soc_dist_1_bin_recode = 
          case_when(
            Q36a %in% c(1:2) ~ 0,
            Q36a %in% c(3:4) ~ 1,
            TRUE ~ NA_real_),
    
    #########################  
    ### SOCIAL DISTANCE 2 ###  
    #########################
    
      # original question number; question text; response options (input above)
        resp_soc_dist_2_qinfo = "NUM: Q36 [Q36c]; QTEXT: I'd like you to rate some different groups of people in (survey country) according to how you feel about them. For each group, please tell me whether your opinion is very favorable, mostly favorable, mostly unfavorable or very unfavorable. Muslims.; ROPTIONS: 1 = Very favorable [=0] + 2 = Mostly favorable [=0] + 3 = Mostly unfavorable [=1] + 4 = Very unfavorable [=1]; TARGET: Muslim; TYPE: Favorability",
      
      # original response (as character vector)
        resp_soc_dist_2_original = 
          dplyr::recode(
            as.character(Q36c),
            "8" = NA_character_,
            "9" = NA_character_),       

      # binary recode (numeric: 1 = any negative attitude expressed; 0 otherwise)
        resp_soc_dist_2_bin_recode = 
          case_when(
            Q36c %in% c(1:2) ~ 0,
            Q36c %in% c(3:4) ~ 1,
            TRUE ~ NA_real_),
    
    ############################  
    ### GENERAL SOCIAL TRUST ###  
    ############################
    
      # original question number; question text; response options (input above)
        resp_gentrust_qinfo = NA, # checked; no question asked

      # original response (as character vector)
        resp_gentrust_original = NA,       

      # binary recode (numeric: 1 = any negative attitude expressed; 0 otherwise)
        resp_gentrust_bin_recode = NA,    
    
    #########################  
    ##### RELIGIOSITY #######  
    #########################
    
      # original question number; question text; response options (input above)
        resp_religiosity_qinfo = "NUM: Q112; QTEXT: How important is religion in your life – very important, somewhat important, not too important, or not at all important?; ROPTIONS: 1 = Very important + 2 = Somewhat important + 3 = Not too important + 4 = Not at all important",
  
      # original response (as numeric vector, with non-substantive responses coded as NA_real_)
        resp_religiosity_original = 
          dplyr::recode(
            as.numeric(q112),
            `8` = NA_real_,
            `9` = NA_real_),       

      # recode (numeric: scaled 0-1, where 1 is more religious)
        resp_religiosity_recode = (4 - resp_religiosity_original)/3
    
    ) %>% 
    select(starts_with("resp_"))
```

# Clean Pew Global Attitudes Spring 2019

```{r}
# clean pew global attitudes spring 2019
  n13_clean_pew_gap_spring_19 <-
    pew_gap_spring_19_raw %>% 
    mutate(
      
    #########################  
    ####### META-DATA #######  
    #########################      
      
      # source name (character vector, title case)
        resp_source = "Pew Global Attitudes Project",
        
      # round number (character vector, title case)  
        resp_round = "Spring 2019",
      
      # url to dataset source, where publicly available (character vector)
        resp_original_data_url = "pewrsr.ch/3wToJeP",

      # survey mode (in-person/phone/internet/mail)
        resp_survey_mode =
          case_when(
            country %in% c(2, 5, 7, 8, 15, 20, 27, 28, 29, 33, 34) ~ "phone",
            TRUE ~ "in-person"),

      # country (character vector; list of countries as written in original source)
        resp_country_original = to_character(country),

      # country (character vector; converts to countrycode county.name list)
        resp_country_common = 
          countryname(resp_country_original),
        
      # interview date (variable of class Date; if only month given, input 1st of month)
        resp_interview_date = as.Date(QDATE_E),
   
    #########################  
    ##### DEMOGRAPHICS ######  
    #########################

      # respondent denomination (character vector, recoded jointly below)
        across(starts_with("d_relig_"), to_character)) %>% 
    mutate(
        d_relig_us = case_when(d_relig_us2 == "Yes" ~ "Christian", TRUE ~ d_relig_us)) %>% 
    select(-c("D_RELIG2_INDIA_A", "D_RELIGION2_JAPAN_A", "D_RELIG_ISRAEL2")) %>%
    mutate(
        resp_denomination = coalesce(!!! select(., starts_with("D_RELIG"))),         

      # respondent's age (character vector; bins denoted by single dash ["18-25"])
        resp_age =
          dplyr::recode(
            as.character(AGE),
            "97" = "97+",
            "98" = NA_character_,
            "99" = NA_character_),        
      
      # respondent's education level
        resp_education_original =
          case_when(
            #Argentina
            D_EDUC_ARGENTINA_2017 == 1 ~ "1. No formal education (preschool or less) [No education]",
            D_EDUC_ARGENTINA_2017 == 2 ~ "2. Incomplete primary [No education]",
            D_EDUC_ARGENTINA_2017 == 3 ~ "3. Complete primary [Primary]",
            D_EDUC_ARGENTINA_2017 == 4 ~ "4. Incomplete secondary [Primary]",
            D_EDUC_ARGENTINA_2017 == 5 ~ "5. Complete secondary [Primary]",
            D_EDUC_ARGENTINA_2017 == 6 ~ "6. Incomplete tertiary, no university [Primary]",
            D_EDUC_ARGENTINA_2017 == 7 ~ "7. Incomplete university [Primary]",
            D_EDUC_ARGENTINA_2017 == 8 ~ "8. Complete tertiary, no university [Primary]",
            D_EDUC_ARGENTINA_2017 == 9 ~ "9. Complete university [College]",
            D_EDUC_ARGENTINA_2017 == 10 ~ "10. Postgraduate (Master) [College]",
            D_EDUC_ARGENTINA_2017 == 11 ~ "11. Postgraduate (Doctorate) [College]",
            D_EDUC_ARGENTINA_2017 %in% c(98, 99) ~ NA_character_,
            #Australia
            D_EDUC_AUSTRALIA_2017 == 1 ~ "6. Year 12 or equivalent (Higher School Certificate HSC or equivalent) (usually 18 years old) [Primary]",
            D_EDUC_AUSTRALIA_2017 == 2 ~ "5. Year 11 or equivalent (usually 17 years old) [Primary]",
            D_EDUC_AUSTRALIA_2017 == 3 ~ "4. Year 10 or equivalent (School Certificate SC or equivalent) (usually 16 years old) [Primary]",
            D_EDUC_AUSTRALIA_2017 == 4 ~ "3. Year 9 or equivalent (usually 15 years old) [Primary]",
            D_EDUC_AUSTRALIA_2017 == 5 ~ "2. Year 8 or below (usually 14 years old) [Primary]",
            D_EDUC_AUSTRALIA_2017 == 6 ~ "1. Did not go to school [No education]",
            D_EDUC_AUSTRALIA_2017 %in% c(8, 9) ~ NA_character_,
            #Brazil
            D_EDUC_BRAZIL == 1 ~ "1. Did not receive formal education [No education]",
            D_EDUC_BRAZIL == 2 ~ "2. Incomplete primary/elementary (1st to 4th grade) [No education]",
            D_EDUC_BRAZIL == 3 ~ "3. Complete primary/elementary (1st to 4th grade) [Primary]",
            D_EDUC_BRAZIL == 4 ~ "4. Incomplete elementary/middle (5th to 8th/9th grade) [Primary]",
            D_EDUC_BRAZIL == 5 ~ "5. Complete elementary/middle (5th to 8th/9th grade) [Primary]",
            D_EDUC_BRAZIL == 6 ~ "6. Incomplete middle/senior high school [Primary]",
            D_EDUC_BRAZIL == 7 ~ "7. Complete middle/senior high school [Primary]",
            D_EDUC_BRAZIL == 8 ~ "8. Incomplete higher education course (Bachelor’s or technologist) [Primary]",
            D_EDUC_BRAZIL == 9 ~ "9. Complete bachelor's or higher degree [College]",
            D_EDUC_BRAZIL == 10 ~ "10. Complete post-graduation courses (specialization, professional masters, MBA) [College]",
            D_EDUC_BRAZIL == 11 ~ "11. Complete master (strictly speaking, academic) [College]",
            D_EDUC_BRAZIL == 12 ~ "12. Complete doctorate [College]",
            D_EDUC_BRAZIL %in% c(98, 99) ~ NA_character_,
            #Bulgaria
            D_EDUC_BULGARIA == 1 ~ "1. Incomplete primary education [No education]",
            D_EDUC_BULGARIA == 2 ~ "2. Primary education [Primary]",
            D_EDUC_BULGARIA == 3 ~ "3. Lower secondary [Primary]",
            D_EDUC_BULGARIA == 4 ~ "4. Upper secondary general [Primary]",
            D_EDUC_BULGARIA == 5 ~ "5. Upper secondary specialized general – foreign language and science high schools [Primary]",
            D_EDUC_BULGARIA == 6 ~ "6. Upper secondary education in schools on arts and sports [Primary]",
            D_EDUC_BULGARIA == 7 ~ "7. Upper secondary professional [Primary]",
            D_EDUC_BULGARIA == 8 ~ "8. Semi-higher [Primary]",
            D_EDUC_BULGARIA == 9 ~ "9. College [Primary]",
            D_EDUC_BULGARIA == 10 ~ "10. Higher – Bachelor’s degree [College]",
            D_EDUC_BULGARIA == 11 ~ "11. Higher – Master’s degree [College]",
            D_EDUC_BULGARIA == 12 ~ "12. PhD [College]",
            D_EDUC_BULGARIA %in% c(98, 99) ~ NA_character_,
            #Canada
            D_EDUC_CANADA_2017 == 1 ~ "1. No formal schooling (Preschool or Kindergarten) [No education]",
            D_EDUC_CANADA_2017 == 2 ~ "2. Completed elementary school (Grade 6) [Primary]",
            D_EDUC_CANADA_2017 == 3 ~ "3. Some junior high school (Grades 7) [Primary]",
            D_EDUC_CANADA_2017 == 4 ~ "4. Completed junior high school (Grade 8) [Primary]",
            D_EDUC_CANADA_2017 == 5 ~ "5. Some senior high school or vocational secondary school (Grade 9-10 in Quebec/Grade 9-11 in the rest of Canada) [Primary]",
            D_EDUC_CANADA_2017 == 6 ~ "6. Completed secondary school or vocational secondary school (secondary school diploma) (Grade 11 in Quebec/Grade 12 in res [Primary]",
            D_EDUC_CANADA_2017 == 7 ~ "7. Trade certificate/upgrading certificate [Primary]",
            D_EDUC_CANADA_2017 == 8 ~ "8. Undergraduate qualifying program/University Transfer Program (CEGEP in Quebec) [Primary]",
            D_EDUC_CANADA_2017 == 9 ~ "9. Technical diploma/community college diploma or certificate [Primary]",
            D_EDUC_CANADA_2017 == 10 ~ "10. Four year college or university degree/Bachelor’s degree (e.g., BS, BA, AB) [College]",
            D_EDUC_CANADA_2017 == 11 ~ "11. Postgraduate or professional degree, including master’s degree (e.g., MA, MS, PhD, MD, DESS, graduate school) [College]",
            D_EDUC_CANADA_2017 %in% c(98, 99) ~ NA_character_,
            #Czech
            D_EDUC_CZECH == 1 ~ "1. No education [No education]",
            D_EDUC_CZECH == 2 ~ "2. Incomplete primary education [No education]",
            D_EDUC_CZECH == 3 ~ "3. Primary education [Primary]",
            D_EDUC_CZECH == 4 ~ "4. Central inc. Apprenticeship (school) [Primary]",
            D_EDUC_CZECH == 5 ~ "5. Secondary vocational (with GCSE) [Primary]",
            D_EDUC_CZECH == 6 ~ "6. Full secondary vocational (with GCSE) [Primary]",
            D_EDUC_CZECH == 7 ~ "7. Up studies (incl. post-secondary studies) [Primary]",
            D_EDUC_CZECH == 8 ~ "8. Higher professional education (graduation) [Primary]",
            D_EDUC_CZECH == 9 ~ "9. Bachelor (Bc., BcA.) [College]",
            D_EDUC_CZECH == 10 ~ "10. Master (Ing., MD., Mgr., etc.) [College]",
            D_EDUC_CZECH == 11 ~ "11. Doctoral (Ph.D., Th.D., DSc. and others) [College]",
            D_EDUC_CZECH %in% c(98, 99) ~ NA_character_,
            #France
            D_EDUC_FRANCE == 1 ~ "1. No formal schooling (Preschool or Kindergarten) [No education]",
            D_EDUC_FRANCE == 2 ~ "2. Primary school with CEP [Primary]",
            D_EDUC_FRANCE == 3 ~ "3. Undergraduate education at the second level - College (Brevet des collèges) [Primary]", #confusing, but base this on position in list
            D_EDUC_FRANCE == 4 ~ "4. Teaching professional second cycle of secondary education (CAP, BEP, etc.) [Primary]",
            D_EDUC_FRANCE == 5 ~ "5. General education second cycle of secondary education (general and technological Baccalaureate) [Primary]",
            D_EDUC_FRANCE == 6 ~ "6. Education schools and other social formations (Diploma educator monitor, specialized technical educator) [Primary]",
            D_EDUC_FRANCE == 7 ~ "7. Preparation for entry to a selective formation 5B (Prepa entry paramedical and social schools, classes update BTS le [Primary]",
            D_EDUC_FRANCE == 8 ~ "8. Pre-university education (Diploma of capacity rights, access to university degree) [Primary]",
            D_EDUC_FRANCE == 9 ~ "9. University Diploma of Technology (DUT) / Certificate of Higher Technician (BTS), Diploma of crafts (DMA) [Primary]",
            D_EDUC_FRANCE == 10 ~ "10. Undergraduate teaching university (Diploma of General University Studies (DEUG), LICENSE, LICENSE (LMD)) [College]",
            D_EDUC_FRANCE == 11 ~ "11. Graduate education university (CONTROL, Diploma (DESS), Master (LMD), Diploma Engineer, Master’s degree) [College]",
            D_EDUC_FRANCE == 12 ~ "12. Graduate education university (Diploma of Advanced Studies (DEA) [College]",
            D_EDUC_FRANCE == 13 ~ "13. Graduate education university - Doctor [College]",
            D_EDUC_FRANCE %in% c(98, 99) ~ NA_character_,
            #Germany
            D_EDUC_GERMANY_2017 == 1 ~ "1. No schooling, did not finish Primary school [No education]",
            D_EDUC_GERMANY_2017 == 2 ~ "2. Primary or lower secondary school [Primary]",
            D_EDUC_GERMANY_2017 == 3 ~ "3. Secondary school - short track, apprenticeship [Primary]",
            D_EDUC_GERMANY_2017 == 4 ~ "4. Secondary school - long track, technical college, university entrance [Primary]",
            D_EDUC_GERMANY_2017 == 5 ~ "5. Bachelors degree/undergraduate [College]",
            D_EDUC_GERMANY_2017 == 6 ~ "6. Completed six years of study, Masters degree [College]",
            D_EDUC_GERMANY_2017 == 7 ~ "7. PhD, Doctorate [College]",
            D_EDUC_GERMANY_2017 %in% c(8, 9) ~ NA_character_,
            #Greece
            D_EDUC_GREECE == 1 ~ "1. No formal schooling (Preschool or Kindergarten) [No education]",
            D_EDUC_GREECE == 2 ~ "2. Partial primary [No education]",
            D_EDUC_GREECE == 3 ~ "3. Primary school certificate [Primary]",
            D_EDUC_GREECE == 4 ~ "4. 3rd Certificate of Secondary Education (High School) [Primary]",
            D_EDUC_GREECE == 5 ~ "5. Epagelmatiko Lykeio (EPAL), Epagelmatiki Sxoli (EPAS) (Technical Vocational Educational Schools (TES), \032echnical Pro [Primary]",
            D_EDUC_GREECE == 6 ~ "6. Certificate of Secondary Education (high school) [Primary]",
            D_EDUC_GREECE == 7 ~ "7. Vocational Training Institute (IEK), colleges [Primary]",
            D_EDUC_GREECE == 8 ~ "8. Technological Educational Institute (TEI) (Higher tertiary education), Vocational Schools of Higher Education [Primary]",
            D_EDUC_GREECE == 9 ~ "9. Technological Educational Institute (A.TEI) (Higher tertiary education), Schools of Higher Vocational Education [Primary]",
            D_EDUC_GREECE == 10 ~ "10. Panepistimio, Polytechneio, Elliniko Anoikto Panepistimio (EAP) (Upper secondary certificate) (Ptychio) [College]", # unsure about this; look up
            D_EDUC_GREECE == 11 ~ "11. Panepistimio, Polytechneio or Elliniko Anoikto Panepistimio (EAP) (Metaptychiako / Master) [College]",
            D_EDUC_GREECE == 12 ~ "12. Panepistimio, Polytechneio or Elliniko Anoikto Panepistimio (EAP) (Didaktoriko diploma) [College]",
            D_EDUC_GREECE %in% c(98, 99) ~ NA_character_,
            #Hungary
            D_EDUC_HUNGARY == 1 ~ "1. Has not attended school [No education]",
            D_EDUC_HUNGARY == 2 ~ "2. 1st-7th grades of primary school or the equivalent [No education]",
            D_EDUC_HUNGARY == 3 ~ "3. Completed primary education or the equivalent [Primary]",
            D_EDUC_HUNGARY == 4 ~ "4. Apprenticeship, vocational school [Primary]",
            D_EDUC_HUNGARY == 5 ~ "5. Matriculation, completed secondary vocational training [Primary]",
            D_EDUC_HUNGARY == 6 ~ "6. Matriculation, Completed high school [Primary]",
            D_EDUC_HUNGARY == 7 ~ "7. Vocational training based on matriculation without access to higher education, secondary technical school [Primary]",
            D_EDUC_HUNGARY == 8 ~ "8. Accredited post secondary training, higher technical school [Primary]",
            D_EDUC_HUNGARY == 9 ~ "9. College degree or college bachelor degree – BA/BSc [College]",
            D_EDUC_HUNGARY == 10 ~ "10. University degree or university master degree – MA/MSc [College]",
            D_EDUC_HUNGARY == 11 ~ "11. Doctoral degree [College]",
            D_EDUC_HUNGARY %in% c(98, 99) ~ NA_character_,
            #India
            D_EDUC_INDIA == 1 ~ "1. Illiterate [No education]",
            D_EDUC_INDIA == 2 ~ "2. Literate but no formal schooling [No education]",
            D_EDUC_INDIA == 3 ~ "3. Primary Education (primary certificate) [Primary]",
            D_EDUC_INDIA == 4 ~ "4. Upper Primary (Upper Primary Certificate) [Primary]",
            D_EDUC_INDIA == 5 ~ "5. High School; Industrial Training Institute (matriculation certificate or ITI certificate [Primary]",
            D_EDUC_INDIA == 6 ~ "6. Senior Secondary / intermediate (Senior Secondary School Leaving Certificate) [Primary]",
            D_EDUC_INDIA == 7 ~ "7. Any technical or industrial training (diploma or certificate) [Primary]",
            D_EDUC_INDIA == 8 ~ "8. Any professional training (General nursing and Midwifery (GNM); Junior teachers training (diploma)) [Primary]",
            D_EDUC_INDIA == 9 ~ "9. Bachelor (university 1st), or MBBS, LLB [College]",
            D_EDUC_INDIA == 10 ~ "10. Master (university 2nd), or Post graduate diploma [College]",
            D_EDUC_INDIA == 11 ~ "11. Doctor (Tertiary 2nd) [College]",
            D_EDUC_INDIA %in% c(98, 99) ~ NA_character_,
            #Indonesia
            D_EDUC_INDONESIA == 1 ~ "1. No formal schooling (Preschool or Kindergarten) [No education]",
            D_EDUC_INDONESIA == 2 ~ "2. Did not finish elementary school (grade 1-5 SD) or Madrasah Ibtidaiyah [No education]",
            D_EDUC_INDONESIA == 3 ~ "3. Finished elementary school (finishing class 6 SD) or Madrasah Ibtidaiyah [Primary]",
            D_EDUC_INDONESIA == 4 ~ "4. Did not finish first school (class 7-8, SMP); Incomplete junior high school (completed secondary grades 7-8), Madrasah T [Primary]",
            D_EDUC_INDONESIA == 5 ~ "5. Finished Junior High School (SMP, class 9), Madrasah Tsanawiyah or SMP Kejuruan [Primary]",
            D_EDUC_INDONESIA == 6 ~ "6. Did not finish high school (class 10-11, SMA); Incomplete high school (completed high school grades 10-11), Madrasah Ali [Primary]",
            D_EDUC_INDONESIA == 7 ~ "7. Finish High School (class 12, SMA), Madrasah Aliyah or Sekolah Menengah Kejuruan [Primary]",
            D_EDUC_INDONESIA == 8 ~ "8. Diploma programmes (DI thru DIII) [Primary]",
            D_EDUC_INDONESIA == 9 ~ "9. Diploma programmes (DIV) or Program Sarjana (Bachelors Degree) [College]",
            D_EDUC_INDONESIA == 10 ~ "10. Program Spesialis 1  (Graduate diploma (SI)) and  Prfesi Doktor (Medical Doctor Diploma) [College]",
            D_EDUC_INDONESIA == 11 ~ "11. Program Master (Masters degree) [College]",
            D_EDUC_INDONESIA == 12 ~ "12. Program Doctor; Specialist S2 program [College]",
            D_EDUC_INDONESIA %in% c(98, 99) ~ NA_character_,
            #Israel
            D_EDUC_ISRAEL == 1 ~ "1. No formal schooling (Preschool or Kindergarten) [No education]",
            D_EDUC_ISRAEL == 2 ~ "2. Primary school (grades I-VI) [Primary]",
            D_EDUC_ISRAEL == 3 ~ "3. Junior high (grades (VII-IX) [Primary]",
            D_EDUC_ISRAEL == 4 ~ "4. Partial Yeshiva [Primary]",
            D_EDUC_ISRAEL == 5 ~ "5. High school (NO matriculation certificate) [Primary]",
            D_EDUC_ISRAEL == 6 ~ "6. Yeshiva (with NO matriculation certificate) [Primary]",
            D_EDUC_ISRAEL == 7 ~ "7. High school (WITH matriculation certificate) [Primary]",
            D_EDUC_ISRAEL == 8 ~ "8. Yeshiva (with matriculation certificate) [Primary]",
            D_EDUC_ISRAEL == 9 ~ "9. School of Engineering (with a matriculation certificate) [Primary]",
            D_EDUC_ISRAEL == 10 ~ "10. Pre-academic preparatory programmes (matriculation certificate) [Primary]",
            D_EDUC_ISRAEL == 11 ~ "11. Post-secondary (technician, engineer) teacher training (teacher diploma) [Primary]",
            D_EDUC_ISRAEL == 12 ~ "12. Kolel [Primary]",
            D_EDUC_ISRAEL == 13 ~ "13. First degree - B.A. [College]",
            D_EDUC_ISRAEL == 14 ~ "14. Second degree - M.A. [College]",
            D_EDUC_ISRAEL == 15 ~ "15. Doctorate [College]",
            D_EDUC_ISRAEL %in% c(98, 99) ~ NA_character_,
            #Italy
            D_EDUC_ITALY_2017 == 1 ~ "1. No educational qualifications [No education]",
            D_EDUC_ITALY_2017 == 2 ~ "2. Primary School/elementary school [Primary]",
            D_EDUC_ITALY_2017 == 3 ~ "3. Secondary school, first degree/middle school degree [Primary]",
            D_EDUC_ITALY_2017 == 4 ~ "4. Secondary school, second degree/high school diploma/maturity [Primary]",
            D_EDUC_ITALY_2017 == 5 ~ "5. Professional specialization (vocational school diploma, art school diploma, technical school diploma, institute of f [Primary]",
            D_EDUC_ITALY_2017 == 6 ~ "6. Three-year degree [College]",
            D_EDUC_ITALY_2017 == 7 ~ "7. Masters degree/five year degree/old system degree [College]",
            D_EDUC_ITALY_2017 == 8 ~ "8. Specialization postgraduate [College]",
            D_EDUC_ITALY_2017 %in% c(98, 99) ~ NA_character_,
            #Japan
            D_EDUC_JAPAN == 1 ~ "1. No formal schooling (Preschool or Kindergarten) [No education]",
            D_EDUC_JAPAN == 2 ~ "2. Elementary school [Primary]",
            D_EDUC_JAPAN == 3 ~ "3. Lower secundary school [Primary]",
            D_EDUC_JAPAN == 4 ~ "4. College of technology [Primary]",
            D_EDUC_JAPAN == 5 ~ "5. Upper Secondary school [Primary]",
            D_EDUC_JAPAN == 6 ~ "6. Junior college (associate degree), special training college (diploma or advanced diploma) [Primary]",
            D_EDUC_JAPAN == 7 ~ "7. Bachelor's degree [College]",
            D_EDUC_JAPAN == 8 ~ "8. Master's degree or Juris Doctor [College]",
            D_EDUC_JAPAN == 9 ~ "9. Doctors degree [College]",
            D_EDUC_JAPAN %in% c(98, 99) ~ NA_character_,
            #Kenya
            D_EDUC_KENYA == 1 ~ "1. No formal education [No education]",
            D_EDUC_KENYA == 2 ~ "2. Early childhood development programme (ECD) [No education]",
            D_EDUC_KENYA == 3 ~ "3. First stage of primary education (Standards 1 to 6) [Primary]",
            D_EDUC_KENYA == 4 ~ "4. Second stage of primary education (Standards 7 to 8) (KCPE) [Primary]", # check with what we did earlier; but implies second stage = complete
            D_EDUC_KENYA == 5 ~ "5. Youth polytechnics programme (Craft trade certificate) [Primary]",
            D_EDUC_KENYA == 6 ~ "6. Incomplete secondary school [Primary]",
            D_EDUC_KENYA == 7 ~ "7. Secondary education (KCSE) [Primary]",
            D_EDUC_KENYA == 8 ~ "8. High School (Form 6) A level Certificate [Primary]",
            D_EDUC_KENYA == 9 ~ "9. Technical and industrial vocational education training (TIVET) [Primary]",
            D_EDUC_KENYA == 10 ~ "10. Teacher training; Polytechnics programmes [Primary]",
            D_EDUC_KENYA == 11 ~ "11. Bachelor [College]",
            D_EDUC_KENYA == 12 ~ "12. Masters [College]",
            D_EDUC_KENYA == 13 ~ "13. Doctorate [College]",
            D_EDUC_KENYA %in% c(98, 99) ~ NA_character_,
            #Lebanon
            D_EDUC_LEBANON == 1 ~ "1. No formal education [No education]",
            D_EDUC_LEBANON == 2 ~ "2. Did not Complete primary [No education]",
            D_EDUC_LEBANON == 3 ~ "3. Completed primary [Primary]",
            D_EDUC_LEBANON == 4 ~ "4. Did not complete intermediate (Including Technical Vocation BP) [Primary]",
            D_EDUC_LEBANON == 5 ~ "5. Completed intermediate (Brevet) [Primary]",
            D_EDUC_LEBANON == 6 ~ "6. Did not complete secondary (Including Technical Vocation till this level) [Primary]",
            D_EDUC_LEBANON == 7 ~ "7. Completed secondary (General Baccalaureate, Technical vocation TS) [Primary]",
            D_EDUC_LEBANON == 8 ~ "8. College / Did not complete university (Including Technical vocation LT) [Primary]",
            D_EDUC_LEBANON == 9 ~ "9. Completed university [College]",
            D_EDUC_LEBANON == 10 ~ "10. Higher education (Master’s degree, Ph.D., etc.) [College]",
            D_EDUC_LEBANON %in% c(98, 99) ~ NA_character_,
            #Lithuania
            D_EDUC_LITHUANIA == 1 ~ "1. Uncompleted primary [No education]",
            D_EDUC_LITHUANIA == 2 ~ "2. Primary [Primary]",
            D_EDUC_LITHUANIA == 3 ~ "3. Vocational without lower secondary [Primary]",
            D_EDUC_LITHUANIA == 4 ~ "4. Lower secondary (including youth, evening schools) [Primary]",
            D_EDUC_LITHUANIA == 5 ~ "5. Vocational finished along with lower secondary [Primary]",
            D_EDUC_LITHUANIA == 6 ~ "6. Vocational after finishing lower secondary (vocational training with less than 2 years) [Primary]",
            D_EDUC_LITHUANIA == 7 ~ "7. Vocational after finishing lower secondary (vocational training with more than 2 years) [Primary]",
            D_EDUC_LITHUANIA == 8 ~ "8. General upper secondary (including gymnasiums, lyceums and evening schools) [Primary]",
            D_EDUC_LITHUANIA == 9 ~ "9. Vocational after finishing lower secondary along with upper secondary [Primary]",
            D_EDUC_LITHUANIA == 10 ~ "10. Vocational after finishing upper secondary [Primary]",
            D_EDUC_LITHUANIA == 11 ~ "11. Advanced vocational or special secondary after finishing lower secondary (including te\032hnical schools if entered af [Primary]",
            D_EDUC_LITHUANIA == 12 ~ "12. Advanced vocational or special secondary after finishing upper secondary (including technical schools if entered af [Primary]",
            D_EDUC_LITHUANIA == 13 ~ "13. Higher non-university or College [Primary]",
            D_EDUC_LITHUANIA == 14 ~ "14. Higher university(Bachelors’ degree) [College]",
            D_EDUC_LITHUANIA == 15 ~ "15. Higher university integrated studies (including studies before 1991-1992 reform) or equivalent (diploma) level afte [College]",
            D_EDUC_LITHUANIA == 16 ~ "16. Higher university (Master’s degree or residency studies) [College]",
            D_EDUC_LITHUANIA == 17 ~ "17. Doctor or candidate of science [College]",
            D_EDUC_LITHUANIA %in% c(98, 99) ~ NA_character_,
            #Mexico
            D_EDUC_MEXICO_2017 == 1 ~ "1. No formal education [No education]",
            D_EDUC_MEXICO_2017 == 2 ~ "2. Incomplete primary [No education]",
            D_EDUC_MEXICO_2017 == 3 ~ "3. Complete primary [Primary]",
            D_EDUC_MEXICO_2017 == 4 ~ "4. Incomplete secondary [Primary]",
            D_EDUC_MEXICO_2017 == 5 ~ "5. Complete secondary [Primary]",
            D_EDUC_MEXICO_2017 == 6 ~ "6. General bachillerato [Primary]",
            D_EDUC_MEXICO_2017 == 7 ~ "7. Commercial career degree [Primary]",
            D_EDUC_MEXICO_2017 == 8 ~ "8. Technical career degree [Primary]",
            D_EDUC_MEXICO_2017 == 9 ~ "9. Complete preparatory [Primary]",
            D_EDUC_MEXICO_2017 == 10 ~ "10. Complete bachelor’s [College]",
            D_EDUC_MEXICO_2017 == 11 ~ "11. Master's degree [College]",
            D_EDUC_MEXICO_2017 == 12 ~ "12. Doctorate [College]",
            D_EDUC_MEXICO_2017 %in% c(98, 99) ~ NA_character_,
            #Netherlands
            D_EDUC_NETHERLANDS == 1 ~ "1. No formal school (preschool or kindergarten) [No education]",
            D_EDUC_NETHERLANDS == 2 ~ "2. Primary school [Primary]",
            D_EDUC_NETHERLANDS == 3 ~ "3. MBO 1 assistent [Primary]",
            D_EDUC_NETHERLANDS == 4 ~ "4. MAVO / Junior general secondary (VAVO-HAVO/VWO - first three grades) [Primary]",
            D_EDUC_NETHERLANDS == 5 ~ "5. Senior general secondary (HAVO/VWO - second three grades) [Primary]",
            D_EDUC_NETHERLANDS == 6 ~ "6. Intermediate Vocational Training (MBO 2/3) [Primary]",
            D_EDUC_NETHERLANDS == 7 ~ "7. MBO 4 Specialist (specialist training diploma) [Primary]",
            D_EDUC_NETHERLANDS == 8 ~ "8. HBO-einddiploma, WO-bachelor [College]",
            D_EDUC_NETHERLANDS == 9 ~ "9. WO-doctorandus, HBO / WO-master, beroepsdiploma [College]",
            D_EDUC_NETHERLANDS == 10 ~ "10. Doctorate [College]",
            D_EDUC_NETHERLANDS %in% c(98, 99) ~ NA_character_,
            #Nigeria
            D_EDUC_NIGERIA == 1 ~ "1. No formal education [No education]",
            D_EDUC_NIGERIA == 2 ~ "2 Pre-primary education [No education]",
            D_EDUC_NIGERIA == 3 ~ "3. Primary education [Primary]",
            D_EDUC_NIGERIA == 4 ~ "4. Junior secondary [Primary]",
            D_EDUC_NIGERIA == 5 ~ "5. Senior secondary [Primary]",
            D_EDUC_NIGERIA == 6 ~ "6. Secondary technical schools programmes [Primary]",
            D_EDUC_NIGERIA == 7 ~ "7. National Diploma, NCE [Primary]",
            D_EDUC_NIGERIA == 8 ~ "8. Higher National Diploma [Primary]",
            D_EDUC_NIGERIA == 9 ~ "9. Bachelor [College]",
            D_EDUC_NIGERIA == 10 ~ "10. Master [College]",
            D_EDUC_NIGERIA == 11 ~ "11. PhD [College]",
            D_EDUC_NIGERIA %in% c(98, 99) ~ NA_character_,
            #Philippines
            D_EDUC_PHILIPPINES == 1 ~ "1. No formal schooling (Preschool or Kindergarten) [No education]",
            D_EDUC_PHILIPPINES == 2 ~ "2. InComplete elementary [No education]",
            D_EDUC_PHILIPPINES == 3 ~ "3. Finished elementary [Primary]",
            D_EDUC_PHILIPPINES == 4 ~ "4. Incomplete high school (Secondary, General (years 1-3), Junior HS leaving certificate [Primary]",
            D_EDUC_PHILIPPINES == 5 ~ "5. Finished Senior High School, Senior High School leaving certificate [Primary]",
            D_EDUC_PHILIPPINES == 6 ~ "6. Incomplete post-secondary technical vocational program [Primary]",
            D_EDUC_PHILIPPINES == 7 ~ "7. Finished post-secondary technical vocational programs (Certificate of Proficiency / Competency)) [Primary]",
            D_EDUC_PHILIPPINES == 8 ~ "8. Incomplete Bachelor’s degree / Pre-baccalaureate [Primary]",
            D_EDUC_PHILIPPINES == 9 ~ "9. Bachelor's degree [College]",
            D_EDUC_PHILIPPINES == 10 ~ "10. Master s degree and professional degree in medicine, dentistry, engineering, law [College]",
            D_EDUC_PHILIPPINES == 11 ~ "11. Doctorate degree [College]",
            D_EDUC_PHILIPPINES %in% c(98, 99) ~ NA_character_,
            #Poland
            D_EDUC_POLAND_2017 == 1 ~ "1. No formal education or incomplete basic [No education]",
            D_EDUC_POLAND_2017 == 2 ~ "2. Basic - 6 classes, incomplete junior high school, Basic - 8 classes, Incomplete middle school [Primary]",
            D_EDUC_POLAND_2017 == 3 ~ "3. Junior high school [Primary]",
            D_EDUC_POLAND_2017 == 4 ~ "4. Vocational school (2-3 years) [Primary]",
            D_EDUC_POLAND_2017 == 5 ~ "5. Completed secondary school (general) [Primary]",
            D_EDUC_POLAND_2017 == 6 ~ "6. Completed technical school [Primary]",
            D_EDUC_POLAND_2017 == 7 ~ "7. Completed post-secondary school [Primary]",
            D_EDUC_POLAND_2017 == 8 ~ "8. Completed bachelors degree, engineering [College]",
            D_EDUC_POLAND_2017 == 9 ~ "9. Completed university studies (masters) [College]",
            D_EDUC_POLAND_2017 == 10 ~ "10. Doctoral, doctor, professor [College]",
            D_EDUC_POLAND_2017 == 98 ~ NA_character_,
            D_EDUC_POLAND_2017 == 99 ~ NA_character_,
            #Russia
            D_EDUC_RUSSIA_2018 == 1 ~ "1. No formal education [No education]",
            D_EDUC_RUSSIA_2018 == 2 ~ "2. Primary general education (completed 3-4 grades of school or less) [Primary]",
            D_EDUC_RUSSIA_2018 == 3 ~ "3. Completed 5-9 years of high school, but did not receive a high school diploma [Primary]",
            D_EDUC_RUSSIA_2018 == 4 ~ "4. Completed secondary education (10 years in the old system, 11 years in the new system), received secondary education [Primary]",
            D_EDUC_RUSSIA_2018 == 5 ~ "5. Primary vocational education: completed PTU, FZU, FZO, vocational-technical school, without secondary education cert [Primary]",
            D_EDUC_RUSSIA_2018 == 6 ~ "6. Primary vocational education: PTU, vocational-technical school along with secondary education (1-3 years of study), [Primary]",
            D_EDUC_RUSSIA_2018 == 7 ~ "7. Secondary vocational education: completed technical school, college (2-4 years of study) [Primary]",
            D_EDUC_RUSSIA_2018 == 8 ~ "8. Bachelors degree in the new 2-tier system of higher education [College]",
            D_EDUC_RUSSIA_2018 == 9 ~ "9. Specialist diploma (5-6 years of study) in the old 1-tier system of higher education or a masters degree in the new [College]",
            D_EDUC_RUSSIA_2018 == 10 ~ "10. Post-graduate education (graduate school, residency), Academic degree (PhD) [College]",
            D_EDUC_RUSSIA_2018 %in% c(98, 99) ~ NA_character_,
            #South Africa
            D_EDUC_SAFRICA_2017 == 1 ~ "1. No formal education [No education]",
            D_EDUC_SAFRICA_2017 == 2 ~ "2. Incomplete primary school [No education]",
            D_EDUC_SAFRICA_2017 == 3 ~ "3. Complete primary school [Primary]",
            D_EDUC_SAFRICA_2017 == 4 ~ "4. Incomplete lower secondary school [Primary]",
            D_EDUC_SAFRICA_2017 == 5 ~ "5. Completed lower secondary school / General Education and Training Certificate (GETC) [Primary]",
            D_EDUC_SAFRICA_2017 == 6 ~ "6. Incomplete secondary school [Primary]",
            D_EDUC_SAFRICA_2017 == 7 ~ "7. Complete secondary / National Senior Certificate (NSC) [Primary]",
            D_EDUC_SAFRICA_2017 == 8 ~ "8. National certificate (vocational NC(V)2 - 4); Nated courses level 1-3 (N1- N3) [Primary]",
            D_EDUC_SAFRICA_2017 == 9 ~ "9. NATED course (N4) [Primary]",
            D_EDUC_SAFRICA_2017 == 10 ~ "10. NATED course (N5) [Primary]",
            D_EDUC_SAFRICA_2017 == 11 ~ "11. NATED course (N6) [Primary]",
            D_EDUC_SAFRICA_2017 == 12 ~ "12. National Higher Certificate (NC), Advanced Certificate  (AC), [Primary]",
            D_EDUC_SAFRICA_2017 == 13 ~ "13. National Diploma [Primary]",
            D_EDUC_SAFRICA_2017 == 14 ~ "14. National Diploma or National N Diploma [Primary]",
            D_EDUC_SAFRICA_2017 == 15 ~ "15. Bachelors degree [College]",
            D_EDUC_SAFRICA_2017 == 16 ~ "16. Advanced diploma [College]",
            D_EDUC_SAFRICA_2017 == 17 ~ "17. Honours Degree [College]",
            D_EDUC_SAFRICA_2017 == 18 ~ "18. High degree (masters, doctorate) [College]",
            D_EDUC_SAFRICA_2017 == 19 ~ "19. Post graduate Diploma [College]",
            D_EDUC_SAFRICA_2017 == 20 ~ "20. Doctorate degree / Laureatus in Technology (Technikon) [College]",
            D_EDUC_SAFRICA_2017 %in% c(98, 99) ~ NA_character_,
            #South Korea
            D_EDUC_SKOREA_2017 == 1 ~ "1. No formal schooling/kindergarten [No education]",
            D_EDUC_SKOREA_2017 == 2 ~ "2. Incomplete primary education [No education]",
            D_EDUC_SKOREA_2017 == 3 ~ "3. Elementary school [Primary]",
            D_EDUC_SKOREA_2017 == 4 ~ "4. Middle school [Primary]",
            D_EDUC_SKOREA_2017 == 5 ~ "5. Vocational highshool) / Trade high school) [Primary]",
            D_EDUC_SKOREA_2017 == 6 ~ "6. General high school) [Primary]",
            D_EDUC_SKOREA_2017 == 7 ~ "7. Junior college, polytechnic college / associate's degree, college of taxation (2-3 years) [Primary]",
            D_EDUC_SKOREA_2017 == 8 ~ "8. Bachelor's degree (4-6 years) [College]",
            D_EDUC_SKOREA_2017 == 9 ~ "9. Master's degree [College]",
            D_EDUC_SKOREA_2017 == 10 ~ "10. Doctor's degree [College]",
            D_EDUC_SKOREA_2017 %in% c(98, 99) ~ NA_character_,
            #Slovakia
            D_EDUC_SLOVAKIA == 1 ~ "1. No formal education [No education]",
            D_EDUC_SLOVAKIA == 2 ~ "2. Unfinished basic education [No education]",
            D_EDUC_SLOVAKIA == 3 ~ "3. Completed basic education [Primary]",
            D_EDUC_SLOVAKIA == 4 ~ "4. Training or secondary education without a leaving certificate [Primary]",
            D_EDUC_SLOVAKIA == 5 ~ "5. Secondary education with a GCSE [Primary]",
            D_EDUC_SLOVAKIA == 6 ~ "6. Unfinished bachelor or higher vocational education [Primary]",
            D_EDUC_SLOVAKIA == 7 ~ "7. Completed bachelor or higher vocational education [College]",
            D_EDUC_SLOVAKIA == 8 ~ "8. Unfinished university education [College]", # must refer to higher degrees (there are multiple stages in the slovak system)
            D_EDUC_SLOVAKIA == 9 ~ "9. Completed university education [College]",
            D_EDUC_SLOVAKIA %in% c(98, 99) ~ NA_character_,
            #Spain
            D_EDUC_SPAIN == 1 ~ "1. No formal education, can not read or write (illiterate) [No education]",
            D_EDUC_SPAIN == 2 ~ "2. Less than 5 years of schooling / Childhood Education [No education]",
            D_EDUC_SPAIN == 3 ~ "3. Did not finish Primary school [No education]",
            D_EDUC_SPAIN == 4 ~ "4. Certificate of Primary / First grade studies (EGB 1 School Certificate Stage 1 plus or minus 10 years) [Primary]",
            D_EDUC_SPAIN == 5 ~ "5. Second grade junior (School graduate or EGB stage 2 1st or 2nd year of junior under 14) [Primary]",
            D_EDUC_SPAIN == 6 ~ "6. Vocational average degree (certification) [Primary]",
            D_EDUC_SPAIN == 7 ~ "7. Second degree second stage (I and II FP Top Bachelor BUP 3rd and 4th year second cycle COU PREU 1st and 2nd Baccalau [Primary]",
            D_EDUC_SPAIN == 8 ~ "8. Training cycles advanced (senior technician) [Primary]",
            D_EDUC_SPAIN == 9 ~ "9. University degree (university diploma, technical architect and engineer) [College]",
            D_EDUC_SPAIN == 10 ~ "10. 1st Degree (Bachelor, Degree) [College]",
            D_EDUC_SPAIN == 11 ~ "11. Master degree [College]",
            D_EDUC_SPAIN == 12 ~ "12. Official qualifications for graduate studies (PhD, medical specialties) [College]",
            D_EDUC_SPAIN %in% c(98, 99) ~ NA_character_,
            #Sweden
            D_EDUC_SWEDEN == 1 ~ "1. No formal school (preschool or kindergarten) [No education]",
            D_EDUC_SWEDEN == 2 ~ "2. Grundskolan (compulsory school) grades 1-6, including adult basic education [Primary]",
            D_EDUC_SWEDEN == 3 ~ "3. Grundskolan (compulsory school) grades 7-9 [Primary]",
            D_EDUC_SWEDEN == 4 ~ "4. Upper secondary school, including folkhögskolan, gymnasieskolan [Primary]",
            D_EDUC_SWEDEN == 5 ~ "5. National state schools for adults – post-secondary training programmes, preparatory (Centrum for flexibelt larande – [Primary]",
            D_EDUC_SWEDEN == 6 ~ "6. Advanced vocational training, 2-3 years [Primary]",
            D_EDUC_SWEDEN == 7 ~ "7. University diploma, general or professional [College]",
            D_EDUC_SWEDEN == 8 ~ "8. Bachelor’s degree [College]",
            D_EDUC_SWEDEN == 9 ~ "9. Master’s degree, general or professional [College]",
            D_EDUC_SWEDEN == 10 ~ "10. Licentiate or Doctorate [College]",
            D_EDUC_SWEDEN %in% c(98, 99) ~ NA_character_,
            #Tunisia
            D_EDUC_TUNISIA_2017 == 1 ~ "1. No formal education (illiterate) [No education]",
            D_EDUC_TUNISIA_2017 == 2 ~ "2. Quranic study/kindergarten, preparatory [No education]",
            D_EDUC_TUNISIA_2017 == 3 ~ "3. Primary stage [Primary]",
            D_EDUC_TUNISIA_2017 == 4 ~ "4. Preparatory (intermediate) stage [Primary]",
            D_EDUC_TUNISIA_2017 == 6 ~ "5. Secondary (First general preparatory stage) [Primary]",
            D_EDUC_TUNISIA_2017 == 7 ~ "6. Secondary (Baccalaureate)-Specialized stage [Primary]",
            D_EDUC_TUNISIA_2017 == 8 ~ "7. Professional Apptitude Certificate (CAP) [Primary]",
            D_EDUC_TUNISIA_2017 == 9 ~ "8. Patent of Professional Technician (BTP) [Primary]",
            D_EDUC_TUNISIA_2017 == 10 ~ "9. Patent of Higher Technician (BTS) [Primary]",
            D_EDUC_TUNISIA_2017 == 12 ~ "10. Bachelors degree (Foundational Applied Degree\\licence) [College]",
            D_EDUC_TUNISIA_2017 == 13 ~ "11. Masters [College]",
            D_EDUC_TUNISIA_2017 == 14 ~ "12. Masters [College]", # likely error; should be doctorate, presumably
            D_EDUC_TUNISIA_2017 %in% c(98, 99) ~ NA_character_,
            #Turkey
            D_EDUC_TURKEY == 1 ~ "1. No formal education [No education]",
            D_EDUC_TURKEY == 2 ~ "2. Incomplete primary school (completed less than 5 grades) [No education]",
            D_EDUC_TURKEY == 3 ~ "3. Primary school (completed 5 grades) [Primary]",
            D_EDUC_TURKEY == 4 ~ "4. Primary school (completed 8 grades) [Primary]",
            D_EDUC_TURKEY == 5 ~ "5. Complete junior high school [Primary]",
            D_EDUC_TURKEY == 6 ~ "6. Complete vocational school at junior high school level [Primary]",
            D_EDUC_TURKEY == 7 ~ "7. Complete high school [Primary]",
            D_EDUC_TURKEY == 8 ~ "8. Complete vocational school at high school level [Primary]",
            D_EDUC_TURKEY == 9 ~ "9. University/College graduates (completed 2 years of college or 4 years of university studies) [College]",
            D_EDUC_TURKEY == 10 ~ "10. Master (completed 2 more years after university) [College]",
            D_EDUC_TURKEY == 11 ~ "11. PhD (completed 4 to 6 more years after university) [College]",
            D_EDUC_TURKEY %in% c(98, 99) ~ NA_character_,
            #UK
            D_EDUC_UK_2017 == 1 ~ "1. No qualifications obtained [Primary]",
            D_EDUC_UK_2017 == 2 ~ "2. Primary school / infant & junior schools [Primary]",
            D_EDUC_UK_2017 == 3 ~ "3. GCSE /CSE/GCE O-level (any grades); Scottish SCE Ordinary Bands; Scottish School Leaving Certificate Lower Grade; Scotti [Primary]",
            D_EDUC_UK_2017 == 4 ~ "4. AA-levels; S-levels; A2-level; GCE Applied A-level; NVQ/SVQ Levels 1-3; Scottish Highers; Scottish SCE/SLC/SUPE at Higher [Primary]",
            D_EDUC_UK_2017 == 5 ~ "5. NVQ 4, Higher National Certificate (HNC) [Primary]",
            D_EDUC_UK_2017 == 6 ~ "6. Foundation degree/HND/Higher Education Diploma/ NVQ5 [Primary]",
            D_EDUC_UK_2017 == 7 ~ "7. University (Bachelor) [College]",
            D_EDUC_UK_2017 == 8 ~ "8. University (Masters degree) [College]",
            D_EDUC_UK_2017 == 9 ~ "9. University (Doctorate, PHD) [College]",
            D_EDUC_UK_2017 %in% c(98, 99) ~ NA_character_,
            #Ukraine
            D_EDUC_UKRAINE == 1 ~ "1. No formal education [No education]",
            D_EDUC_UKRAINE == 2 ~ "2. Unfinished primary education [No education]",
            D_EDUC_UKRAINE == 3 ~ "3. Completed primary education [Primary]",
            D_EDUC_UKRAINE == 4 ~ "4. Unfinished secondary education [Primary]",
            D_EDUC_UKRAINE == 5 ~ "5. Completed secondary education [Primary]",
            D_EDUC_UKRAINE == 6 ~ "6. Unfinished vocational/specialized secondary [Primary]",
            D_EDUC_UKRAINE == 7 ~ "7. Completed vocational/specialized secondary education [Primary]",
            D_EDUC_UKRAINE == 8 ~ "8. Unfinished higher education [Primary]",
            D_EDUC_UKRAINE == 9 ~ "9. Completed higher education [College]",
            D_EDUC_UKRAINE == 10 ~ "10. Academic degree (Ph.D., academician) [College]",
            D_EDUC_UKRAINE %in% c(98, 99) ~ NA_character_,
            #US
            d_educ_us_2017 == 1 ~ "1. Less than high school (Grades 1 thru 8 or no formal schooling) [Primary]",
            d_educ_us_2017 == 2 ~ "2. High school incomplete (Grades 9-11 or Grade 12 with NO diploma) [Primary]",
            d_educ_us_2017 == 3 ~ "3. High school graduate (Grade 12 with diploma or GED certificate) [Primary]",
            d_educ_us_2017 == 4 ~ "4. Some college, no degree (includes some community college) [Primary]",
            d_educ_us_2017 == 5 ~ "5. Two year associate degree from a college or university [Primary]",
            d_educ_us_2017 == 6 ~ "6. Four year college or university degree/Bachelor’s degree (e.g., BS, BA, AB) [College]",
            d_educ_us_2017 == 7 ~ "7. Some postgraduate or professional schooling, no postgraduate degree (e.g. some graduate school) [College]",
            d_educ_us_2017 == 8 ~ "8. Postgraduate or professional degree, including master’s, doctorate, medical or law degree (e.g., MA, MS, PhD, MD, JD, gr [College]",
            d_educ_us_2017 %in% c(98, 99) ~ NA_character_),       
      
      # respondent's gender (numeric: female = 1; male = 0; other = NA)
        resp_female =
          case_when(
            SEX == 1 ~ 0,
            SEX == 2 ~ 1),
      
      # respondent resident in rural (vs urban) area (numeric: rural = 1; urban/semi-urban/peri-urban = 0)
        resp_rural =
          case_when(
            D_DENSITY %in% c(1:3) ~ 0,
            D_DENSITY %in% c(4:5) ~ 1,
            TRUE ~ NA_real_),
      
    #########################  
    ### SOCIAL DISTANCE 1 ###  
    #########################
    
      # original question number; question text; response options (input above)
        resp_soc_dist_1_qinfo = "NUM: Q48 [FAV_JEWS91]; QTEXT: I'd like you to rate some different groups of people in (survey country) according to how you feel about them. For each group, please tell me whether your opinion of them is very favorable, mostly favorable, mostly unfavorable or very unfavorable. How about Jews?; ROPTIONS: 1 = Very favorable [=0] + 2 = Mostly favorable [=0] + 3 = Mostly unfavorable [=1] + 4 = Very unfavorable [=1]; TARGET: Jewish; TYPE: Favorability",
      
      # original response (as character vector)
        resp_soc_dist_1_original = 
          dplyr::recode(
            as.character(FAV_JEWS91),
            "8" = NA_character_,
            "9" = NA_character_),       

      # binary recode (numeric: 1 = any negative attitude expressed; 0 otherwise)
        resp_soc_dist_1_bin_recode = 
          case_when(
            FAV_JEWS91 %in% c(1:2) ~ 0,
            FAV_JEWS91 %in% c(3:4) ~ 1,
            TRUE ~ NA_real_),
    
    #########################  
    ### SOCIAL DISTANCE 2 ###  
    #########################
    
      # original question number; question text; response options (input above)
        resp_soc_dist_2_qinfo = "NUM: Q48 [FAV_MUSLIMS_COUNTRY]; QTEXT: I'd like you to rate some different groups of people in (survey country) according to how you feel about them. For each group, please tell me whether your opinion of them is very favorable, mostly favorable, mostly unfavorable or very unfavorable. How about Muslims?; ROPTIONS: 1 = Very favorable [=0] + 2 = Mostly favorable + 3 = Mostly unfavorable [=1] + 4 = Very unfavorable; TARGET: Muslim; TYPE: Favorability",
      
      # original response (as character vector)
        resp_soc_dist_2_original = 
          dplyr::recode(
            as.character(FAV_MUSLIMS_COUNTRY),
            "8" = NA_character_,
            "9" = NA_character_),       

      # binary recode (numeric: 1 = any negative attitude expressed; 0 otherwise)
        resp_soc_dist_2_bin_recode = 
          case_when(
            FAV_MUSLIMS_COUNTRY %in% c(1:2) ~ 0,
            FAV_MUSLIMS_COUNTRY %in% c(3:4) ~ 1,
            TRUE ~ NA_real_),
    
    #########################  
    ### SOCIAL DISTANCE 3 ###  
    #########################
    
      # original question number; question text; response options (input above)
        resp_soc_dist_3_qinfo = "NUM: Q48 [fav_muslimbulg]; QTEXT: I'd like you to rate some different groups of people in (survey country) according to how you feel about them. For each group, please tell me whether your opinion of them is very favorable, mostly favorable, mostly unfavorable or very unfavorable. How about Muslim Bulgarians?; ROPTIONS: 1 = Very favorable [=0] + 2 = Mostly favorable + 3 = Mostly unfavorable [=1] + 4 = Very unfavorable; TARGET: Muslim; TYPE: Favorability",
      
      # original response (as character vector)
        resp_soc_dist_3_original = 
          dplyr::recode(
            as.character(fav_muslimbulg),
            "8" = NA_character_,
            "9" = NA_character_),       

      # binary recode (numeric: 1 = any negative attitude expressed; 0 otherwise)
        resp_soc_dist_3_bin_recode = 
          case_when(
            fav_muslimbulg %in% c(1:2) ~ 0,
            fav_muslimbulg %in% c(3:4) ~ 1,
            TRUE ~ NA_real_),
    
    ############################  
    ### GENERAL SOCIAL TRUST ###  
    ############################
    
      # original question number; question text; response options (input above)
        resp_gentrust_qinfo = NA, #checked; no question asked

      # original response (as character vector)
       resp_gentrust_original = NA,          

      # binary recode (numeric: 1 = any negative attitude expressed; 0 otherwise)
        resp_gentrust_bin_recode = NA,    
    
    #########################  
    ##### RELIGIOSITY #######  
    #########################
    
      # original question number; question text; response options (input above)
        resp_religiosity_qinfo = "NUM: Q85 [RELIGION_IMPORT]; QTEXT: How important is religion in your life – very important, somewhat important, not too important, or not at all important?; ROPTIONS: 1 = Very important + 2 = Somewhat important + 3 = Not too important + 4 = Not at all important",
  
      # original response (as numeric vector, with non-substantive responses coded as NA_real_)
        resp_religiosity_original = 
          dplyr::recode(
            as.numeric(RELIGION_IMPORT),
            `8` = NA_real_,
            `9` = NA_real_),       

      # recode (numeric: scaled 0-1, where 1 is more religious)
        resp_religiosity_recode = (4 - resp_religiosity_original)/3
    
    ) %>% 
    select(starts_with("resp_"))
```

# Stack

```{r}
# stack
  clean_pew_gap <-
    n1_clean_pew_gap_spring_04 %>%
    bind_rows(n2_clean_pew_gap_spring_05_11countries) %>%
    bind_rows(n3_clean_pew_gap_spring_05_6countries) %>%
    bind_rows(n4_clean_pew_gap_spring_08) %>%
    bind_rows(n5_clean_pew_gap_spring_09) %>%
    bind_rows(n6_clean_pew_gap_fall_09) %>%
    bind_rows(n7_clean_pew_gap_spring_10) %>%
    bind_rows(n8_clean_pew_gap_spring_11) %>%
    bind_rows(n9_clean_pew_gap_spring_14) %>%
    bind_rows(n10_clean_pew_gap_spring_15) %>%
    bind_rows(n11_clean_pew_gap_spring_16) %>%
    bind_rows(n13_clean_pew_gap_spring_19) %>% # note, number 12 deleted (see note on Pew Global Attitudes Spring 2017, above)
    bind_rows(n14_clean_pew_gap_spring_06) 
```

# Merge in resp_religion variable (based on manual recoding)

```{r}
# extract unique resp_denomination
  # unique_denom <- 
  #   clean_pew_gap %>%
  #   mutate(denom_id = group_indices(., resp_denomination)) %>%
  #   distinct(denom_id, resp_denomination) %>% 
  #   arrange(resp_denomination) %>% 
  #   select(denom_id, resp_denomination) %>% 
  #   write.csv("pga_coding_resp_religion.csv")

# merge
  clean_pew_gap_relig_merged <- 
    clean_pew_gap %>%
    mutate(resp_denomination = str_trim(resp_denomination)) %>% 
    left_join(
      cw_resp_religion %>% 
      mutate(resp_denomination = str_trim(resp_denomination)), 
      by = "resp_denomination") %>% 
    select(-denom_id) %>%
    mutate(
      resp_religion = 
        case_when(
          resp_religion == "" ~ NA_character_,
          resp_religion == "NA_character_" ~ NA_character_,
          TRUE ~ resp_religion))

# 3 countries/rounds are missing resp_denomination data; these are indicated and confirmed in the code above
  # clean_pew_gap_relig_merged %>% 
  #   filter(is.na(resp_denomination)) %>% 
  #   pull(resp_round) %>% 
  #   table()
```

# Save

```{r}
  saveRDS(clean_pew_gap_relig_merged, "../cleaned-data/y-1-multi-pew-global-attitudes.rds")
```
